﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
33614	Django's object creation when using GenericForeignKeys results in different ContentType model id's	Lucas Zocco	nobody	"I've created a Document model that has a GenericFK to another model, however I have noticed that the related_type id is different depending on how the object is created. It seems to be wrong if done in the manner explained in the official documentation (https://docs.djangoproject.com/en/4.0/ref/contrib/contenttypes/#generic-relations-1) where the object is directly assigned to related_object instead of creating the GenericForeignKey by setting the related_type and related_id individually.

A small code snippet to illustrate:
{{{
class Document(BaseModel):
    related_id = models.UUIDField(
        null=True,
        blank=True,
        db_index=True,
        help_text=(""The UUID of the origin model, which this flag came from.""),
    )
    related_type = models.ForeignKey(
        ContentType,
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        help_text=""Type that the Subject id relates to"",
    )
    related = GenericForeignKey(""related_type"", ""related_id"")

doc1 = Document(data=b'',
            related=company)

doc2 = Document(data=b'',
            related_type=ContentType.objects.get_for_model(company),
            related_id=company.id)

print(ContentType.objects.get_for_model(company).id)
>>> 23

print(doc1.related_type.id)
>>> 148

print(doc2.related_type.id)
>>> 23
}}}

For reference: There's no object with id 148 in the ContentTypes table so I'm not sure where it's getting it from."	Bug	closed	contrib.contenttypes	4.0	Normal	invalid			Unreviewed	0	0	0	0	0	0
