Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.6.x] Fixed #17528 -- Documented that add() and remove() with a man…

…y-to-many relationship do not call Model.save()

Backport of 3cdeb57 from master
  • Loading branch information...
commit 1c8e539a7723fe2d76b5c5a8b83c8a3da2d9fc39 1 parent 9bb04b1
@timgraham timgraham authored
Showing with 16 additions and 0 deletions.
  1. +16 −0 docs/ref/models/relations.txt
View
16 docs/ref/models/relations.txt
@@ -48,6 +48,14 @@ Related objects reference
>>> e = Entry.objects.get(id=234)
>>> b.entry_set.add(e) # Associates Entry e with Blog b.
+ In the example above, ``e.save()`` is called to perform the update.
+ 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()
+ <django.db.models.query.QuerySet.bulk_create>`. If you need to execute
+ some custom logic when a relationship is created, listen to the
+ :data:`~django.db.models.signals.m2m_changed` signal.
+
.. method:: create(**kwargs)
Creates a new object, saves it and puts it in the related object set.
@@ -86,6 +94,14 @@ Related objects reference
>>> e = Entry.objects.get(id=234)
>>> b.entry_set.remove(e) # Disassociates Entry e from Blog b.
+ Similar to :meth:`add()`, ``e.save()`` is called in the example above
+ to perform the update. Using ``remove()`` with a many-to-many
+ relationship, however, will delete the relationships using
+ :meth:`QuerySet.delete()<django.db.models.query.QuerySet.delete>` which
+ means no model ``save()`` methods are called; listen to the
+ :data:`~django.db.models.signals.m2m_changed` signal if you wish to
+ execute custom code when a relationship is deleted.
+
For :class:`~django.db.models.ForeignKey` objects, this method only
exists if ``null=True``. If the related field can't be set to ``None``
(``NULL``), then an object can't be removed from a relation without
Please sign in to comment.
Something went wrong with that request. Please try again.