Opened 15 years ago

Closed 12 years ago

Last modified 12 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

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)

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

Download all attachments as: .zip

Change History (9)

comment:1 by sebastian.hillig, 14 years ago

Triage Stage: UnreviewedAccepted

comment:2 by sebastian.hillig, 14 years ago

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

Stumbled over the same behaviour.

comment:3 by Alexandre Garnier, 14 years ago

Same for me

in reply to:  3 comment:4 by phretor, 13 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 Julien Phalip, 13 years ago

Severity: Normal
Type: New feature

comment:6 by FunkyBob, 12 years ago

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

by FunkyBob, 12 years ago

Attachment: 11850-test.patch added

Patch to test ordering

comment:7 by anonymous, 12 years ago

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 by Chris Beaven, 12 years ago

(Oops, that was me closing)

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