Opened 4 years ago

Closed 4 years ago

#17299 closed Bug (invalid)

The proxy model does not allow one to use ManyToMany fields

Reported by: anonymous Owned by: nobody
Component: Database layer (models, ORM) Version: 1.3
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


Suppose I have an app where the users may write messages. Then suppose that users must be able to follow one another in a twitter-like fashion. As far as I see, the most clean solution will be to create a non-symmetrical self-referencing m2m relation on the contrib.auth.models.User. Editing the code in contrib.auth.models.User itself will be naturally be an outrage, so I'll need to define a model inheriting from User and define the m2m relation there but this will lead to the creation of a new DB table for the descendant which will contain only the id field. In order to avoid this, one would suppose that he should be able to define the descendant as a proxy model, as the new m2m relation doesn't really affect the database representation of contrib.auth.User besides creating foreign keys to the intermediary model.

Unfortunately, the following code will result in "django.core.exceptions.FieldError: Proxy model 'MyUser' contains model fields":

from django.contrib.auth import models

class MyUser(models.User):
    subscription = models.ManyToMany('self', symmetrical=False)

    class Meta:
        proxy = True

Change History (1)

comment:1 Changed 4 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

By definition, proxy models can't define additional fields, so you're seeing the expected behavior.

Indeed, from the database's point of view, this restriction could be relaxed for ManyToMany fields. But that would have consequences in the ORM. This would be a major feature request, and I'm afraid its complexity would outweigh its advantages.

If you're interested in this feature, you should write a proposal to the django-developers mailing list, preferably with a proof of concept patch.

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