Opened 10 years ago

Closed 8 years ago

Last modified 8 years ago

#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



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):

class Group(models.Model):
    name = models.CharField(max_length=128)
    members = models.ManyToManyField(Person, through='Membership')

    def __unicode__(self):

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)

11850-test.patch (2.0 KB) - added by FunkyBob 8 years ago.
Patch to test ordering

Download all attachments as: .zip

Change History (9)

comment:1 Changed 10 years ago by sebastian.hillig

Triage Stage: UnreviewedAccepted

comment:2 Changed 10 years ago by sebastian.hillig

Owner: changed from nobody to sebastian.hillig
Status: newassigned

Stumbled over the same behaviour.

comment:3 Changed 10 years ago by Alexandre Garnier

Same for me

comment:4 in reply to:  3 Changed 9 years ago by phretor

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 Changed 9 years ago by Julien Phalip

Severity: Normal
Type: New feature

comment:6 Changed 8 years ago by FunkyBob

Cc: FunkyBob 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 :)

Changed 8 years ago by FunkyBob

Attachment: 11850-test.patch added

Patch to test ordering

comment:7 Changed 8 years ago by anonymous

Resolution: wontfix
Status: assignedclosed

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.

comment:8 Changed 8 years ago by Chris Beaven

(Oops, that was me closing)

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