0

So I have added a many to many relationship to my model:

class Person(models.Model):
    ...
    ...
    cars = models.ManyToManyField(Car)

When I do:

bob.add(mercedes)
bob.add(bmw)

And then do person.cars.all(), I get:

[bmw, mercedes]

I looked in the many-to-many table and the order there is correct. So it's something like:

id    person    car 
1     bob       mercedes
2     bob       bmw

So all I need is to sort by many-to-many table's id column when retrieving cars. How to do it?

Richard Knop
  • 81,041
  • 149
  • 392
  • 552

1 Answers1

1

I think Ordering many-to-many relations in Django models has th answer you need.

You basically have to manually specify the through table and guarantee the ordering in there.

Community
  • 1
  • 1
Ben
  • 6,687
  • 2
  • 33
  • 46