#30460: ManyToMany relationships with a custom `through` do not respect
Meta.ordering on the intermediary model
-------------------------------------+-------------------------------------
               Reporter:             |          Owner:  nobody
  ryanpetrello                       |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  2.2
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 `ManyToManyField`s that specify an intermediary model with a custom
 `Meta.ordering` do not inherit that ordering.

 {{{#!python
 class Dog(models.Model):
     pass


 class OrderedMembership(models.Model):

     class Meta:
         ordering = ('position',)

     dog = models.ForeignKey('Dog', models.CASCADE)
     winner = models.ForeignKey('Winner', models.CASCADE)
     position = models.PositiveIntegerField()


 class Winner(models.Model):
     name = models.CharField(max_length=128)
     # OrderedMembership object defined as a class
     members = models.ManyToManyField(Dog, through=OrderedMembership)

 lassie = Dog('Lassie')
 fido = Dog('Fido')
 rover = Dog('Rover')
 dog_show = Winner(name='Dog Show')
 OrderedMembership.objects.create(dog=lassie, winner=dog_show, position=3)
 OrderedMembership.objects.create(dog=fido, winner=dog_show, position=2)
 OrderedMembership.objects.create(dog=rover, winner=dog_show, position=1)

 dog_show.members.all()   # <--- does not respect
 `OrderedMembership.position`


 }}}

 This means that if you want to create a custom M2M model that manages
 default order via some column on the M2M table:

 https://github.com/ansible/awx/pull/3842/files#diff-
 ea6da88b8c0cd3fbf7858ae87933b296R995
 https://github.com/gregmuellegger/django-
 sortedm2m/blob/b48481ebd1212f1af22a6a04d1c8372b5a837350/sortedm2m/fields.py#L41

 ...it doesn't work as you'd expect (the M2M relation falls back to natural
 database order - usually the primary key of the target model).

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30460>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/055.e64aa7aaffe41b71d65e2533e37188ad%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to