mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #7596. Added Model.objects.bulk_create, and make use of it in several places. This provides a performance benefit when inserting multiple objects. THanks to Russ for the review, and Simon Meers for the MySQl implementation.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16739 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -1158,6 +1158,29 @@ has a side effect on your data. For more, see `Safe methods`_ in the HTTP spec.
|
||||
|
||||
.. _Safe methods: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.1
|
||||
|
||||
bulk_create
|
||||
~~~~~~~~~~~
|
||||
|
||||
.. method:: bulk_create(objs)
|
||||
|
||||
This method inserts the provided list of objects into the database in an
|
||||
efficient manner (generally only 1 query, no matter how many objects there
|
||||
are)::
|
||||
|
||||
>>> Entry.objects.bulk_create([
|
||||
... Entry(headline="Django 1.0 Released"),
|
||||
... Entry(headline="Django 1.1 Announced"),
|
||||
... Entry(headline="Breaking: Django is awesome")
|
||||
... ])
|
||||
|
||||
This has a number of caveats though:
|
||||
|
||||
* The model's ``save()`` method will not be called, and the ``pre_save`` and
|
||||
``post_save`` signals will not be sent.
|
||||
* It does not work with child models in a multi-table inheritance scenario.
|
||||
* If the model's primary key is an :class:`~django.db.models.AutoField` it
|
||||
does not retrieve and set the primary key attribute, as ``save()`` does.
|
||||
|
||||
count
|
||||
~~~~~
|
||||
|
||||
|
||||
@@ -252,6 +252,17 @@ filename. For example, the file ``css/styles.css`` would also be saved as
|
||||
See the :class:`~django.contrib.staticfiles.storage.CachedStaticFilesStorage`
|
||||
docs for more information.
|
||||
|
||||
``Model.objects.bulk_create`` in the ORM
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
This method allows for more efficient creation of multiple objects in the ORM.
|
||||
It can provide significant performance increases if you have many objects,
|
||||
Django makes use of this internally, meaning some operations (such as database
|
||||
setup for test suites) has seen a performance benefit as a result.
|
||||
|
||||
See the :meth:`~django.db.models.query.QuerySet.bulk_create` docs for more
|
||||
information.
|
||||
|
||||
Minor features
|
||||
~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
@@ -268,3 +268,33 @@ instead of::
|
||||
|
||||
entry.blog.id
|
||||
|
||||
Insert in bulk
|
||||
==============
|
||||
|
||||
When creating objects, where possible, use the
|
||||
:meth:`~django.db.models.query.QuerySet.bulk_create()` method to reduce the
|
||||
number of SQL queries. For example::
|
||||
|
||||
Entry.objects.bulk_create([
|
||||
Entry(headline="Python 3.0 Released"),
|
||||
Entry(headline="Python 3.1 Planned")
|
||||
])
|
||||
|
||||
Is preferable to::
|
||||
|
||||
Entry.objects.create(headline="Python 3.0 Released")
|
||||
Entry.objects.create(headline="Python 3.1 Planned")
|
||||
|
||||
Note that there are a number of :meth:`caveats to this method
|
||||
<django.db.models.query.QuerySet.bulk_create>`, make sure it is appropriate for
|
||||
your use case. This also applies to :class:`ManyToManyFields
|
||||
<django.db.models.ManyToManyField>`, doing::
|
||||
|
||||
my_band.members.add(me, my_friend)
|
||||
|
||||
Is preferable to::
|
||||
|
||||
my_band.members.add(me)
|
||||
my_band.members.add(my_friend)
|
||||
|
||||
Where ``Bands`` and ``Artists`` have a many-to-many relationship.
|
||||
|
||||
Reference in New Issue
Block a user