Skip to content

Instantly share code, notes, and snippets.

@rbeucher
Forked from Attila03/FK.py
Created May 14, 2019 11:06
Show Gist options
  • Save rbeucher/bae5792840faf26437063b9435f60a4b to your computer and use it in GitHub Desktop.
Save rbeucher/bae5792840faf26437063b9435f60a4b to your computer and use it in GitHub Desktop.
Django bulk insert with Foreign Key
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