10👍
Unfortunately, batch inserts are something that Django 1.3 and prior do not directly support. If you want to use the ORM, then you do have to call save() on each individual object. If it’s a large list and performance is an issue, you can use django.db.cursor to INSERT the items manually inside a transaction to dramatically speed the process up. If you have a huge dataset, you need to start looking at Database engine specific methods, like COPY FROM in Postgres.
38👍
As of Django 1.4, there exists a bulk_create()
method on the QuerySet object, which allows for inserting a list of objects in a single query. For more info, see:
- [Django]-How to do a multi-step form in Django?
- [Django]-Removing help_text from Django UserCreateForm
- [Django]-How to add an HTTP header to all Django responses
5👍
From Django 1.4 exists bulk_create()
, but, always but.
You need to be careful, using bulk_create()
it wont call instance save()
method internally.
As django docs says
The model’s save() method will not be called
So, if you are overriding save method, (as my case was) you can’t use bulk_create.
- [Django]-Django Sessions
- [Django]-Django ModelForm to have a hidden input
- [Django]-Django – limiting query results
2👍
This question is also addressed in How do I perform a batch insert in Django?, which provides some ways to make Django do this.
- [Django]-Django REST Framework how to specify error code when raising validation error in serializer
- [Django]-Right way to return proxy model instance from a base model instance in Django?
- [Django]-Error: can't start new thread
1👍
This might be a good starting point, but as the author of the code snippet says, it might not be production ready.
- [Django]-Django/Python assertRaises with message check
- [Django]-Django Custom User Email Account Verification
- [Django]-Create django super user in a docker container without inputting password