-
-
Save rbeucher/bae5792840faf26437063b9435f60a4b to your computer and use it in GitHub Desktop.
Django bulk insert with Foreign Key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from stocks import wsgi | |
from django.core.exceptions import ObjectDoesNotExist | |
from api.models import Stock, Company | |
import csv | |
csvpath = r'prices.csv' | |
batch_size = 500 | |
t1 = time.time() | |
with open(csvpath) as csvfile: | |
reader = csv.DictReader(csvfile) | |
stocks = [] | |
for i, row in enumerate(reader): | |
try: | |
company = Company.objects.get(symbol=row['symbol']) | |
except ObjectDoesNotExist as e: | |
company = None | |
s = Stock(date=row['date'],company=company,....) | |
stocks.append(s) | |
if i % batch_size == 0: | |
Stock.objects.bulk_create(stocks) | |
stocks = [] | |
Stock.objects.bulk_create(stocks) | |
t2 = time.time() | |
print(t2-t1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment