Created
February 19, 2018 05:37
-
-
Save Attila03/4c491b336117cb86f524cb2d1ed42ab1 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
Exelent, thanks very much.!