79๐
Use bulk_create()
method. Itโs standard in Django now.
Example:
Entry.objects.bulk_create([
Entry(headline="Django 1.0 Released"),
Entry(headline="Django 1.1 Announced"),
Entry(headline="Breaking: Django is awesome")
])
132๐
as of the django development, there exists bulk_create
as an object manager method which takes as input an array of objects created using the class constructor. check out django docs
- [Django]-Using django-rest-interface
- [Django]-Cross domain at axios
- [Django]-Django MultiValueDictKeyError error, how do I deal with it
8๐
name = request.data.get('name')
period = request.data.get('period')
email = request.data.get('email')
prefix = request.data.get('prefix')
bulk_number = int(request.data.get('bulk_number'))
bulk_list = list()
for _ in range(bulk_number):
code = code_prefix + uuid.uuid4().hex.upper()
bulk_list.append(
DjangoModel(name=name, code=code, period=period, user=email))
bulk_msj = DjangoModel.objects.bulk_create(bulk_list)
- [Django]-How to delete a record in Django models?
- [Django]-Speeding up Django Testing
- [Django]-Django: Redirect to previous page after login
5๐
worked for me to use manual transaction handling for the loop(postgres 9.1):
from django.db import transaction
with transaction.atomic():
for item in items:
MyModel.objects.create(name=item.name)
in fact itโs not the same, as โnativeโ database bulk insert, but it allows you to avoid/descrease transport/orms operations/sql query analyse costs
- [Django]-How to get the currently logged in user's id in Django?
- [Django]-NumPy array is not JSON serializable
- [Django]-Django: Grab a set of objects from ID list (and sort by timestamp)
4๐
Here is how to bulk-create entities from column-separated file, leaving aside all unquoting and un-escaping routines:
SomeModel(Model):
@classmethod
def from_file(model, file_obj, headers, delimiter):
model.objects.bulk_create([
model(**dict(zip(headers, line.split(delimiter))))
for line in file_obj],
batch_size=None)
- [Django]-FileUploadParser doesn't get the file name
- [Django]-Django index page best/most common practice
- [Django]-Django models.py Circular Foreign Key
3๐
Using create will cause one query per new item. If you want to reduce the number of INSERT queries, youโll need to use something else.
Iโve had some success using the Bulk Insert snippet, even though the snippet is quite old.
Perhaps there are some changes required to get it working again.
- [Django]-.filter() vs .get() for single object? (Django)
- [Django]-Django change default runserver port
- [Django]-Django AutoField with primary_key vs default pk
2๐
Check out this blog post on the bulkops module.
On my django 1.3 app, I have experienced significant speedup.
- [Django]-Django select_for_update cannot be used outside of a transaction
- [Django]-Django create userprofile if does not exist
- [Django]-How to test "render to template" functions in django? (TDD)
-29๐
The easiest way is to use the create
Manager method, which creates and saves the object in a single step.
for item in items:
MyModel.objects.create(name=item.name)
- [Django]-Unique fields that allow nulls in Django
- [Django]-Python (and Django) best import practices
- [Django]-How to use "get_or_create()" in Django?