Bulk creating model objects in django

I have many objects to save in the database, so I want to create Model instances with this.

With django, I can create all instances of models with MyModel(data) , and then I want to save all of them.

I currently have something like this:

 for item in items: object = MyModel(name=item.name) object.save() 

I am wondering if I can save a list of objects directly, for example:

 objects = [] for item in items: objects.append(MyModel(name=item.name)) objects.save_all() 

How to save all objects in one transaction?

+69
django django-models
Aug 31 '10 at 11:23
source share
8 answers

since version 1.4 of django, bulk_create exists as an object manager method that takes as input an array of objects created using the class constructor. check django docs

+77
Jan 16 2018-12-12T00:
source share

Use bulk_create() . This is 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") ]) 
+22
Jan 26 '16 at 22:43
source share

worked for me to use manual transaction processing for the loop (postgres 9.1):

 from django.db import transaction with transaction.commit_on_success(): for item in items: MyModel.objects.create(name=item.name) 

in fact, this is not the same as the built-in embedded database, but it allows you to avoid / reduce the transfer / spelling operations / analyze the costs of the sql query.

+4
Nov 08
source share

Here's how to massively create entities from a column delimited file, leaving aside all fuzzy and non-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) 
+3
Apr 29 '13 at 11:03
source share

to implement a single line you can use the lambda expression in the map

 map(lambda x:MyModel.objects.get_or_create(name=x), items) 

Here lambda corresponds to each element in the list of elements for x and, if necessary, creates a database record.

Lambda documentation

+2
Aug 31 '10 at 12:14
source share

Using create will trigger a single request for a new item. If you want to reduce the number of INSERT requests, you will need to use something else.

I had some success using the Bulk Insert fragment, although the fragment is quite old. Perhaps there are some changes needed to make it work again.

http://djangosnippets.org/snippets/446/

+2
Sep 02 '10 at 23:01
source share

Check out this bulkops blog post .

In my django 1.3 application, I experienced significant acceleration.

+2
Mar 19 2018-12-12T00:
source share

The easiest way is to use the create Manager method, which creates and saves the object in one step.

 for item in items: MyModel.objects.create(name=item.name) 
-17
Aug 31 '10 at 11:45
source share



All Articles