#33614: Django's object creation when using GenericForeignKeys results in 
different
ContentType model id's
------------------------------------------------+------------------------
               Reporter:  Lucas Zocco           |          Owner:  nobody
                   Type:  Bug                   |         Status:  new
              Component:  contrib.contenttypes  |        Version:  4.0
               Severity:  Normal                |       Keywords:
           Triage Stage:  Unreviewed            |      Has patch:  0
    Needs documentation:  0                     |    Needs tests:  0
Patch needs improvement:  0                     |  Easy pickings:  0
                  UI/UX:  0                     |
------------------------------------------------+------------------------
 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.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33614>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107017fe5695408-4efc818b-a82c-463d-9c47-dcaf41f554ab-000000%40eu-central-1.amazonses.com.

Reply via email to