115
113
Takes a set of valid :ref:`lookup arguments <field-lookups-intro>` for the
116
114
model the :class:`~django.contrib.contenttypes.models.ContentType`
117
represents, and does :ref:`a get() lookup <get-kwargs>` on that model,
115
represents, and does :lookup:`a get() lookup <get>` on that model,
118
116
returning the corresponding object.
120
118
.. method:: models.ContentType.model_class()
324
322
... object_id=b.id)
325
323
[<TaggedItem: django>, <TaggedItem: python>]
327
Note that if the model with a :class:`~django.contrib.contenttypes.generic.GenericForeignKey`
328
that you're referring to uses a non-default value for ``ct_field`` or ``fk_field``
329
(e.g. the :mod:`django.contrib.comments` app uses ``ct_field="object_pk"``),
330
you'll need to pass ``content_type_field`` and ``object_id_field`` to
331
:class:`~django.contrib.contenttypes.generic.GenericRelation`.::
333
comments = generic.GenericRelation(Comment, content_type_field="content_type", object_id_field="object_pk")
335
Note that if you delete an object that has a
325
Note that if the model in a
326
:class:`~django.contrib.contenttypes.generic.GenericRelation` uses a
327
non-default value for ``ct_field`` or ``fk_field`` in its
328
:class:`~django.contrib.contenttypes.generic.GenericForeignKey` (e.g. the
329
:mod:`django.contrib.comments` app uses ``ct_field="object_pk"``),
330
you'll need to set ``content_type_field`` and/or ``object_id_field`` in
331
the :class:`~django.contrib.contenttypes.generic.GenericRelation` to
332
match the ``ct_field`` and ``fk_field``, respectively, in the
333
:class:`~django.contrib.contenttypes.generic.GenericForeignKey`::
335
comments = generic.GenericRelation(Comment, object_id_field="object_pk")
337
Note also, that if you delete an object that has a
336
338
:class:`~django.contrib.contenttypes.generic.GenericRelation`, any objects
337
339
which have a :class:`~django.contrib.contenttypes.generic.GenericForeignKey`
338
340
pointing at it will be deleted as well. In the example above, this means that
342
344
Generic relations and aggregation
343
345
---------------------------------
345
:ref:`Django's database aggregation API <topics-db-aggregation>`
347
:doc:`Django's database aggregation API </topics/db/aggregation>`
346
348
doesn't work with a
347
349
:class:`~django.contrib.contenttypes.generic.GenericRelation`. For example, you
348
350
might be tempted to try something like::
361
363
:class:`~django.contrib.contenttypes.generic.GenericInlineFormSet`
362
364
and :class:`~django.contrib.contenttypes.generic.GenericInlineModelAdmin`.
363
365
This enables the use of generic relations in forms and the admin. See the
364
:ref:`model formset <topics-forms-modelforms>` and
365
:ref:`admin <ref-contrib-admin>` documentation for more information.
366
:doc:`model formset </topics/forms/modelforms>` and
367
:doc:`admin </ref/contrib/admin/index>` documentation for more information.
367
369
.. class:: generic.GenericInlineModelAdmin
369
371
The :class:`~django.contrib.contenttypes.generic.GenericInlineModelAdmin`
370
372
class inherits all properties from an
371
:class:`~django.contrib.admin.options.InlineModelAdmin` class. However,
373
:class:`~django.contrib.admin.InlineModelAdmin` class. However,
372
374
it adds a couple of its own for working with the generic relation:
374
376
.. attribute:: generic.GenericInlineModelAdmin.ct_field