Skip to content

Instantly share code, notes, and snippets.

@Attila03
Created February 19, 2018 05:37
Show Gist options
  • Save Attila03/4c491b336117cb86f524cb2d1ed42ab1 to your computer and use it in GitHub Desktop.
Save Attila03/4c491b336117cb86f524cb2d1ed42ab1 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)
@alejandrohdo
Copy link

Exelent, thanks very much.!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment