#11850 closed New feature (wontfix)
Intermediate model order not applied to related model
Reported by: | pielgrzym | Owned by: | sebastian.hillig |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.1 |
Severity: | Normal | Keywords: | model ordering, many to many, through |
Cc: | FunkyBob | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Hi,
Here is a slightly modified example from django m2m with intermediate model docs (note the meta class with order in Membership model):
class Person(models.Model): name = models.CharField(max_length=128) def __unicode__(self): return self.name class Group(models.Model): name = models.CharField(max_length=128) members = models.ManyToManyField(Person, through='Membership') def __unicode__(self): return self.name class Membership(models.Model): person = models.ForeignKey(Person) group = models.ForeignKey(Group) date_joined = models.DateField() invite_reason = models.CharField(max_length=64) class Meta: ordering = ('date_joined')
Now if we try to access the group members (as in django docs):
>>> beatles.members.all() [<Person: Ringo Starr>, <Person: Paul McCartney>]
They are sorted with order of Person model, not intermediate model. You have to manually assign order_by to achieve the desired effect:
>>> beatles.members.all().order_by('membership__date_joined')
It would be nice to be able to specify the intermediate order as default order for ManyRelatedManager.
Attachments (1)
Change History (9)
comment:1 by , 15 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 15 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:4 by , 14 years ago
It looks like that
>>> beatles.membership_set.all()
is correctly ordered. Not intuitive, but at least you don't have to write the ordering criterion twice.
comment:5 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → New feature |
comment:6 by , 13 years ago
Cc: | added |
---|---|
Easy pickings: | unset |
Keywords: | ordering through added; orderin removed |
UI/UX: | unset |
It would be nice if the Many-to-Many field were ordered by the through model's odering [if set]. Comment 4 above only gets you the intermediate model, whereas I want to get a set of models at the other end of the M2M. I would formulate a patch to do this, but the ORM code is deep magic to me -- I wouldn't know where to start :)
comment:7 by , 13 years ago
Resolution: | → wontfix |
---|---|
Status: | assigned → closed |
Given the description example, the sets ordering will be based on Person
's Meta
ordering. Changing this to use the intermediary table's ordering would be backwards incompatible.
We discussed in IRC having an ordering=(...)
parameter for the m2m field, but that should live in a new ticket.
Stumbled over the same behaviour.