From 2308fb5806bd30befc9f076905c1fd865e1176bf Mon Sep 17 00:00:00 2001 From: Steven DeMartini Date: Thu, 28 Apr 2022 12:15:40 -0700 Subject: [PATCH] [4.0.x] Refs #23435 -- Added note about GenericForeignKey indexes to docs. Backport of 562e3bc09aa094a2ebbd3890fa233d04daafa8c9 from main --- docs/ref/contrib/contenttypes.txt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/ref/contrib/contenttypes.txt b/docs/ref/contrib/contenttypes.txt index 7550f4fc8d..2740afede3 100644 --- a/docs/ref/contrib/contenttypes.txt +++ b/docs/ref/contrib/contenttypes.txt @@ -255,6 +255,11 @@ For example, it could be used for a tagging system like so:: def __str__(self): return self.tag + class Meta: + indexes = [ + models.Index(fields=["content_type", "object_id"]), + ] + A normal :class:`~django.db.models.ForeignKey` can only "point to" one other model, which means that if the ``TaggedItem`` model used a :class:`~django.db.models.ForeignKey` it would have to @@ -285,6 +290,14 @@ model: :class:`~django.contrib.contenttypes.fields.GenericForeignKey` will look for. + Unlike for the :class:`~django.db.models.ForeignKey`, a database index is + *not* automatically created on the + :class:`~django.contrib.contenttypes.fields.GenericForeignKey`, so it's + recommended that you use + :attr:`Meta.indexes ` to add your own + multiple column index. This behavior :ticket:`may change <23435>` in the + future. + .. attribute:: GenericForeignKey.for_concrete_model If ``False``, the field will be able to reference proxy models. Default