Django: Ordering by the first value of a tuple
I have a 7-tuple of tuples as such:
POSSIBILITIES = ((1, "Something"),
(2, "Something else"), ...)
Now I have an IntegerField
with cho开发者_StackOverflow社区ices
in a model with the possibilities listed above.
class Something(models.Model):
class Meta:
ordering = "...?"
something = models.IntegerField(choices=POSSIBILITIES)
I want the entries in the database to be ordered by the integer in each of the tuples by default. How do I specify that?
This should do the trick:
class Meta:
ordering = ('something',)
The last comma is important, it is required.
ordering = ('something',)
should work. The integer values are what is actually stored for something
in the database, so they would be ordered by the integers by default.
精彩评论