Opened 5 years ago

Last modified 5 years ago

#31498 closed Bug

GenericRelation's get_internal_type() incorrectly reports "ManyToManyField". — at Initial Version

Reported by: George Dorn Owned by: nobody
Component: contrib.contenttypes Version: 3.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have two models, one containing a GFK to the other (and presumably others models).

Code highlighting:

class Example(models.Model):
    text = models.TextField()
    example_gfk = GenericRelation(
        "myapp.ExampleGFK",
        related_query_name="example",
    )

class ExampleGFK(Bulkable):
    text = models.TextField()
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content = GenericForeignKey("content_type", "object_id")

When I inspect Example._meta.get_field('example_gfk').get_internal_type(), it reports back a ManyToManyField. This can't be right, though; as a reverse relationship, it should be a ManyToOneRel; an ExampleGFK can't be related to more than one Example object.

Perhaps there's a solid reason for this that eludes me, like it tricks some part of the admin or other part of the ORM to behave correctly. If so, at the very least a note in the documentation or a docstring in the code could clarify that reason.

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top