mirror of
https://github.com/django/django.git
synced 2024-12-01 15:42:04 +01:00
Removed a redundant example in contenttypes docs.
This commit is contained in:
parent
85ef98dc6e
commit
aa69f36984
@ -395,6 +395,14 @@ from ``TaggedItem``::
|
|||||||
>>> TaggedItem.objects.filter(bookmarks__url__contains='django')
|
>>> TaggedItem.objects.filter(bookmarks__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
|
||||||
|
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`
|
Just as :class:`~django.contrib.contenttypes.fields.GenericForeignKey`
|
||||||
accepts the names of the content-type and object-ID fields as
|
accepts the names of the content-type and object-ID fields as
|
||||||
arguments, so too does
|
arguments, so too does
|
||||||
@ -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
|
``object_primary_key`` to create its generic foreign key, then a
|
||||||
:class:`.GenericRelation` back to it would need to be defined like so::
|
:class:`.GenericRelation` back to it would need to be defined like so::
|
||||||
|
|
||||||
tags = GenericRelation(TaggedItem,
|
tags = GenericRelation(
|
||||||
content_type_field='content_type_fk',
|
TaggedItem,
|
||||||
object_id_field='object_primary_key')
|
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")
|
|
||||||
|
|
||||||
Note also, that if you delete an object that has a
|
Note also, that if you delete an object that has a
|
||||||
:class:`~django.contrib.contenttypes.fields.GenericRelation`, any objects
|
:class:`~django.contrib.contenttypes.fields.GenericRelation`, any objects
|
||||||
|
Loading…
Reference in New Issue
Block a user