mirror of
https://github.com/django/django.git
synced 2025-08-03 10:34:04 +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:
parent
e55bbf4c3c
commit
7deb25b8dd
22 changed files with 331 additions and 78 deletions
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue