[Answered ]-Django: can I add a list of new model instances to the database via a single method call?



Edit: Unfortunately this is not on 1.3

Original Answer

Thank god for bulk_create!

You could then do something like this:


For those too lazy to click the link, here is the example from the docs:

>>> Entry.objects.bulk_create([
...     Entry(headline="Django 1.0 Released"),
...     Entry(headline="Django 1.1 Announced"),
...     Entry(headline="Breaking: Django is awesome")
... ])


I believe Brandon Konkle’s reply to a similar question is still valid: Question about batch save objects in Django

In summary: Sadly, no, you’ll have to use django.db.cursor with a manual query to do so. If the dataset is small, or the performance is of less importance though, looping through isn’t really THAT bad, and is the simplest solution.

Also, see this ticket: https://code.djangoproject.com/ticket/661


Leave a comment