Skip to content

Instantly share code, notes, and snippets.

@mglowinski93
Last active June 5, 2023 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mglowinski93/e6838702e601407ba00156f575494445 to your computer and use it in GitHub Desktop.
Save mglowinski93/e6838702e601407ba00156f575494445 to your computer and use it in GitHub Desktop.
Example of Django import using generator and bulk_create
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