Opened 5 years ago

Closed 5 years ago

#31498 closed Bug (wontfix)

GenericRelation's get_internal_type() incorrectly reports "ManyToManyField".

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 (last modified by George Dorn)

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 it is a ManyToManyField. This can't be right, though; as a reverse relationship, it should be a ManyToOneRel; a single 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 (2)

comment:1 by George Dorn, 5 years ago

Description: modified (diff)

comment:2 by Mariusz Felisiak, 5 years ago

Resolution: wontfix
Status: newclosed

get_internal_type() should return a string naming this field for backend specific purposes. ManyToOneRel stores information about the relation it's not a field. I don't see a better candidate from builtin fields than ManyToManyField. This is in Django from the beginning (see bca5327b21eb2e3ee18292cbe532d6d0071201d8). IMO we should treat this as a implementation detail.

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