[3.1.x] Fixed #32045 -- Doc'd GenericRelatedObjectManager methods.

This also documents that .remove() and clear() methods delete related
objects.

Backport of 354c1524b3 from master
This commit is contained in:
Craig Smith 2020-10-20 16:36:03 +11:00 committed by Mariusz Felisiak
parent 8b3010a298
commit d67f965c79
1 changed files with 30 additions and 0 deletions

View File

@ -391,6 +391,36 @@ be used to retrieve their associated ``TaggedItems``::
>>> b.tags.all() >>> b.tags.all()
<QuerySet [<TaggedItem: django>, <TaggedItem: python>]> <QuerySet [<TaggedItem: django>, <TaggedItem: python>]>
You can also use ``add()``, ``create()``, or ``set()`` to create
relationships::
>>> t3 = TaggedItem(tag='Web development')
>>> b.tags.add(t3, bulk=False)
>>> b.tags.create(tag='Web framework')
<TaggedItem: Web framework>
>>> b.tags.all()
<QuerySet [<TaggedItem: django>, <TaggedItem: python>, <TaggedItem: Web development>, <TaggedItem: Web framework>]>
>>> b.tags.set([t1, t3])
>>> b.tags.all()
<QuerySet [<TaggedItem: django>, <TaggedItem: Web development>]>
The ``remove()`` call will bulk delete the specified model objects::
>>> b.tags.remove(t3)
>>> b.tags.all()
<QuerySet [<TaggedItem: django>]>
>>> TaggedItem.objects.all()
<QuerySet [<TaggedItem: django>]>
The ``clear()`` method can be used to bulk delete all related objects for an
instance::
>>> b.tags.clear()
>>> b.tags.all()
<QuerySet []>
>>> TaggedItem.objects.all()
<QuerySet []>
Defining :class:`~django.contrib.contenttypes.fields.GenericRelation` with Defining :class:`~django.contrib.contenttypes.fields.GenericRelation` with
``related_query_name`` set allows querying from the related object:: ``related_query_name`` set allows querying from the related object::