mirror of https://github.com/django/django.git
Corrected GenericRelation's related_query_name manual lookup example.
And changed related_query_name to a singular noun.
This commit is contained in:
parent
d15c61cabb
commit
130192b12b
|
@ -394,21 +394,21 @@ be used to retrieve their associated ``TaggedItems``::
|
||||||
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::
|
||||||
|
|
||||||
tags = GenericRelation(TaggedItem, related_query_name='bookmarks')
|
tags = GenericRelation(TaggedItem, related_query_name='bookmark')
|
||||||
|
|
||||||
This enables filtering, ordering, and other query operations on ``Bookmark``
|
This enables filtering, ordering, and other query operations on ``Bookmark``
|
||||||
from ``TaggedItem``::
|
from ``TaggedItem``::
|
||||||
|
|
||||||
>>> # Get all tags belonging to bookmarks containing `django` in the url
|
>>> # Get all tags belonging to bookmarks containing `django` in the url
|
||||||
>>> TaggedItem.objects.filter(bookmarks__url__contains='django')
|
>>> TaggedItem.objects.filter(bookmark__url__contains='django')
|
||||||
<QuerySet [<TaggedItem: django>, <TaggedItem: python>]>
|
<QuerySet [<TaggedItem: django>, <TaggedItem: python>]>
|
||||||
|
|
||||||
Of course, if you don't add the reverse relationship, you can do the
|
Of course, if you don't add the ``related_query_name``, you can do the
|
||||||
same types of lookups manually::
|
same types of lookups manually::
|
||||||
|
|
||||||
>>> b = Bookmark.objects.get(url='https://www.djangoproject.com/')
|
>>> bookmarks = Bookmark.objects.filter(url__contains='django')
|
||||||
>>> bookmark_type = ContentType.objects.get_for_model(b)
|
>>> bookmark_type = ContentType.objects.get_for_model(Bookmark)
|
||||||
>>> TaggedItem.objects.filter(content_type__pk=bookmark_type.id, object_id=b.id)
|
>>> TaggedItem.objects.filter(content_type__pk=bookmark_type.id, object_id__in=bookmarks)
|
||||||
<QuerySet [<TaggedItem: django>, <TaggedItem: python>]>
|
<QuerySet [<TaggedItem: django>, <TaggedItem: python>]>
|
||||||
|
|
||||||
Just as :class:`~django.contrib.contenttypes.fields.GenericForeignKey`
|
Just as :class:`~django.contrib.contenttypes.fields.GenericForeignKey`
|
||||||
|
|
Loading…
Reference in New Issue