Fixed #18556 -- Allowed RelatedManager.add() to execute 1 query where possible.

Thanks Loic Bistuer for review.
This commit is contained in:
Tim Graham 2015-03-14 21:25:33 -04:00 committed by Loïc Bistuer
parent c2e70f0265
commit adc0c4fbac
8 changed files with 151 additions and 28 deletions

View file

@ -36,7 +36,7 @@ Related objects reference
In this example, the methods below will be available both on
``topping.pizza_set`` and on ``pizza.toppings``.
.. method:: add(*objs)
.. method:: add(*objs, bulk=True)
Adds the specified model objects to the related object set.
@ -48,7 +48,13 @@ Related objects reference
In the example above, in the case of a
:class:`~django.db.models.ForeignKey` relationship,
``e.save()`` is called by the related manager to perform the update.
:meth:`QuerySet.update() <django.db.models.query.QuerySet.update>`
is used to perform the update. This requires the objects to already be
saved.
You can use the ``bulk=False`` argument to instead have the related
manager perform the update by calling ``e.save()``.
Using ``add()`` with a many-to-many relationship, however, will not
call any ``save()`` methods, but rather create the relationships
using :meth:`QuerySet.bulk_create()
@ -56,6 +62,12 @@ Related objects reference
some custom logic when a relationship is created, listen to the
:data:`~django.db.models.signals.m2m_changed` signal.
.. versionchanged:: 1.9
The ``bulk`` parameter was added. In order versions, foreign key
updates were always done using ``save()``. Use ``bulk=False`` if
you require the old behavior.
.. method:: create(**kwargs)
Creates a new object, saves it and puts it in the related object set.
@ -135,7 +147,7 @@ Related objects reference
:class:`~django.db.models.ForeignKey`\s where ``null=True`` and it also
accepts the ``bulk`` keyword argument.
.. method:: set(objs, clear=False)
.. method:: set(objs, bulk=True, clear=False)
.. versionadded:: 1.9
@ -150,6 +162,8 @@ Related objects reference
If ``clear=True``, the ``clear()`` method is called instead and the
whole set is added at once.
The ``bulk`` argument is passed on to :meth:`add`.
Note that since ``set()`` is a compound operation, it is subject to
race conditions. For instance, new objects may be added to the database
in between the call to ``clear()`` and the call to ``add()``.