-
-
Save mglowinski93/e6838702e601407ba00156f575494445 to your computer and use it in GitHub Desktop.
Example of Django import using generator and bulk_create.
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
import csv | |
from itertools import islice | |
from django.db import migrations | |
BATCH_SIZE = 1000 | |
def read_csv_file(file_path: str, batch_size: int): | |
with open(file_path, "r") as f: | |
reader = csv.reader(f) | |
while True: | |
batch = list(islice(reader, batch_size)) | |
if not batch: | |
return | |
yield batch | |
def import_people_records(apps, schema_editor): | |
Person = apps.get_model("people", "Person") | |
for batch in read_csv_file("people.csv", BATCH_SIZE): | |
Person.objects.bulk_create( | |
[ | |
Person( | |
name=name, | |
email=email, | |
) | |
for name, email in batch | |
], | |
batch_size=BATCH_SIZE, | |
) | |
def delete_people_records(apps, schema_editor): | |
apps.get_model("persons", "Person").objects.all().delete() | |
class Migration(migrations.Migration): | |
dependencies = [ | |
("people", "0001_initial"), | |
] | |
operations = [ | |
migrations.RunPython( | |
code=import_people_records, | |
reverse_code=delete_people_records, | |
), | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment