Opened 9 months ago

Closed 9 months ago

#34897 closed Bug (invalid)

Django's admin UI - `ManyToManyField` with inheritance on the same class -> object shows itself inside the relation

Reported by: Leon Haffmans Owned by: nobody
Component: contrib.admin Version: 4.2
Severity: Normal Keywords: inheritance, ManyToMany
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: yes


Consider a base class Followable which is an actual table in the database. Create a User model, which inherits from Followable but also contains a ManyToManyField relating to Followable called follows.

Now, when using Django's admin panel, a user will (wrongly) always show up in follows as reference to itself. This reference has nothing to do with the database, there is no entry in the relation table.

This should serve as a little reproducable example:

class Followable(models.Model):
    # This is necessary to prevent doubled id name in `User`
    followable_id = models.BigAutoField(primary_key=True)

class User(Followable):
    alias = models.CharField(max_length=32)
    follows = models.ManyToManyField(Followable, blank=True, related_name="followed_by")

user = User.objects.get(alias="foo_user")
assert len(user.follows) == 0

If this is a known issue, I apologize. I wasn't able to find posts addressing this issue.

Change History (1)

comment:1 by Leon Haffmans, 9 months ago

Resolution: invalid
Status: newclosed

I'm sorry, I didn't understand the admin UI properly. You can just ignore this.

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