I'm populating a dropdown with values defined in my models.py, something like:
rank_choices = (
('King', 'King')
('Prince', 'Prince')
('Duke', 'Duke')
('Baron', 'Baron')
('Lackey', 'Lackey')
When I'm displaying a list of people with a rank at a particular time, I want them to appear in descending rank.
Current query (merely alphabetic):
attendees = Rank.objects.filter(feast__id=feast__id).exclude(rank='Lackey').order_by('rank')
How could I change this to order rank by its position in the list?
I'm considering this:
rank_choices = (
(0, 'King')
(1, 'Prince')
(2, 'Duke')
(3, 'Baron')
(4, 'Lackey')
But even if I can get the verbose values back from the numeric values, any changes in the ordering would return incorrect values for data pre-change. Is there a better way?
Chosen solution
Inspired by wim's answer, I ended up doing a data migration to change the ranks to numeric values and sorting as follows.
ranks = sorted(ranks, key=lambda x: int(x.rank))
I'm getting the verbose values back by importing the rank_choices from my models into my views and replacing the numeric values with the corresponding titles after the sorting.