Skip to content

Commit

Permalink
Removed a redundant example in contenttypes docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed May 16, 2016
1 parent 85ef98d commit aa69f36
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions docs/ref/contrib/contenttypes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,14 @@ from ``TaggedItem``::
>>> TaggedItem.objects.filter(bookmarks__url__contains='django')
<QuerySet [<TaggedItem: django>, <TaggedItem: python>]>

Of course, if you don't add the reverse relationship, you can do the
same types of lookups manually::

>>> b = Bookmark.objects.get(url='https://www.djangoproject.com/')
>>> bookmark_type = ContentType.objects.get_for_model(b)
>>> TaggedItem.objects.filter(content_type__pk=bookmark_type.id, object_id=b.id)
<QuerySet [<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
Expand All @@ -406,30 +414,11 @@ referred to above used fields named ``content_type_fk`` and
``object_primary_key`` to create its generic foreign key, then a
:class:`.GenericRelation` back to it would need to be defined like so::

tags = GenericRelation(TaggedItem,
content_type_field='content_type_fk',
object_id_field='object_primary_key')

Of course, if you don't add the reverse relationship, you can do the
same types of lookups manually::

>>> b = Bookmark.objects.get(url='https://www.djangoproject.com/')
>>> bookmark_type = ContentType.objects.get_for_model(b)
>>> TaggedItem.objects.filter(content_type__pk=bookmark_type.id,
... object_id=b.id)
<QuerySet [<TaggedItem: django>, <TaggedItem: python>]>

Note that if the model in a
:class:`~django.contrib.contenttypes.fields.GenericRelation` uses a
non-default value for ``ct_field`` or ``fk_field`` in its
:class:`~django.contrib.contenttypes.fields.GenericForeignKey` (for example, if
you had a ``Comment`` model that uses ``ct_field="object_pk"``),
you'll need to set ``content_type_field`` and/or ``object_id_field`` in
the :class:`~django.contrib.contenttypes.fields.GenericRelation` to
match the ``ct_field`` and ``fk_field``, respectively, in the
:class:`~django.contrib.contenttypes.fields.GenericForeignKey`::

comments = fields.GenericRelation(Comment, object_id_field="object_pk")
tags = GenericRelation(
TaggedItem,
content_type_field='content_type_fk',
object_id_field='object_primary_key',
)

Note also, that if you delete an object that has a
:class:`~django.contrib.contenttypes.fields.GenericRelation`, any objects
Expand Down

0 comments on commit aa69f36

Please sign in to comment.