[Answered ]-Django foreign key ordering / position

2👍

While writing the question I stumbled upon a possible solution.

class Item(models.Model):
    list = models.ForeignKey(List)
     # ...

    class Meta:
        order_with_respect_to = 'list'

This model could then be manipulated like so:

list = List.objects.get(id=1)
list.get_item_order() == [1, 2, 3]

The order of a List object’s related Item objects can be set by passing in a list of Item primary keys:

list.set_item_order([3, 1, 2])

Haven’t tried this code so feel free to correct me (comments or edits).

Leave a comment