Code

Opened 5 years ago

Closed 3 years ago

Last modified 3 years ago

#11850 closed New feature (wontfix)

Intermediate model order not applied to related model

Reported by: pielgrzym Owned by: bastih
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 3 years ago.
Patch to test ordering

Download all attachments as: .zip

Change History (9)

comment:1 Changed 5 years ago by bastih

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 5 years ago by bastih

  • Owner changed from nobody to bastih
  • Status changed from new to assigned

Stumbled over the same behaviour.

comment:3 follow-up: Changed 5 years ago by zigarn

Same for me

comment:4 in reply to: ↑ 3 Changed 4 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 3 years ago by julien

  • Severity set to Normal
  • Type set to New feature

comment:6 Changed 3 years ago by FunkyBob

  • Cc FunkyBob added
  • Easy pickings unset
  • Keywords ordering, many, 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 3 years ago by FunkyBob

Patch to test ordering

comment:7 Changed 3 years ago by anonymous

  • Resolution set to wontfix
  • Status changed from assigned to 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.

comment:8 Changed 3 years ago by SmileyChris

(Oops, that was me closing)

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.