diff options
| author | Gabe Jackson <gabejackson@cxg.ch> | 2014-03-04 12:23:32 +0100 |
|---|---|---|
| committer | Anssi Kääriäinen <akaariai@gmail.com> | 2014-03-05 22:37:53 +0200 |
| commit | b77f26313cddbfde20dcf2661e9bd35458c2d1bd (patch) | |
| tree | ed655a90373cf606e304e82eaeceaf025c892328 /docs | |
| parent | c627da0ccc12861163f28177aa7538b420a9d310 (diff) | |
Fixed #22207 -- Added support for GenericRelation reverse lookups
GenericRelation now supports an optional related_query_name argument.
Setting related_query_name adds a relation from the related object back to
the content type for filtering, ordering and other query operations.
Thanks to Loic Bistuer for spotting a couple of important issues in
his review.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/ref/contrib/contenttypes.txt | 23 | ||||
| -rw-r--r-- | docs/releases/1.7.txt | 5 |
2 files changed, 28 insertions, 0 deletions
diff --git a/docs/ref/contrib/contenttypes.txt b/docs/ref/contrib/contenttypes.txt index d01fc6baa5..bc8f538857 100644 --- a/docs/ref/contrib/contenttypes.txt +++ b/docs/ref/contrib/contenttypes.txt @@ -373,6 +373,15 @@ Reverse generic relations This class used to be defined in ``django.contrib.contenttypes.generic``. + .. attribute:: related_query_name + + .. versionadded:: 1.7 + + The relation on the related object back to this object doesn't exist by + default. Setting ``related_query_name`` creates a relation from the + related object back to this one. This allows querying and filtering + from the related object. + If you know which models you'll be using most often, you can also add a "reverse" generic relationship to enable an additional API. For example:: @@ -392,6 +401,20 @@ be used to retrieve their associated ``TaggedItems``:: >>> b.tags.all() [<TaggedItem: django>, <TaggedItem: python>] +.. versionadded:: 1.7 + +Defining :class:`~django.contrib.contenttypes.fields.GenericRelation` with +``related_query_name`` set allows querying from the related object:: + + tags = GenericRelation(TaggedItem, related_query_name='bookmarks') + +This enables filtering, ordering, and other query operations on ``Bookmark`` +from ``TaggedItem``:: + + >>> # Get all tags belonging to books containing `django` in the url + >>> TaggedItem.objects.filter(bookmarks__url__contains='django') + [<TaggedItem: django>, <TaggedItem: python>] + Just as :class:`~django.contrib.contenttypes.fields.GenericForeignKey` accepts the names of the content-type and object-ID fields as arguments, so too does diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt index 4c0457e7c5..6c1ab36c90 100644 --- a/docs/releases/1.7.txt +++ b/docs/releases/1.7.txt @@ -1165,6 +1165,11 @@ Miscellaneous * The ``shortcut`` view in ``django.contrib.contenttypes.views`` now supports protocol-relative URLs (e.g. ``//example.com``). +* :class:`~django.contrib.contenttypes.fields.GenericRelation` now supports an + optional ``related_query_name`` argument. Setting ``related_query_name`` adds + a relation from the related object back to the content type for filtering, + ordering and other query operations. + .. _deprecated-features-1.7: Features deprecated in 1.7 |
