I’m trying to order models in a Django app using a field that has predefined choices. I can sort alphabetically, but since the choices are the days of the week, that doesn’t quite match what I need. Here’s the model:
class Slot( models.Model ):
DAY_CHOICES = (
('SUN', 'Sunday'),
('MON', 'Monday'),
('TUE', 'Tuesday'),
('WED', 'Wednesday'),
('THU', 'Thursday'),
('FRI', 'Friday'),
('SAT', 'Saturday'),
)
day = models.CharField( max_length=3, choices=DAY_CHOICES )
start = models.TimeField()
end = models.TimeField()
template = models.ForeignKey( Template )
And here’s the inline that’s pulling the model into an admin view:
class SlotInline( admin.TabularInline ):
model = Slot
ordering = ('day','start',)
I know I could probably use an integer as the stored value, but is there a general way to impose a custom sort order on a field with choices?
Quoting from the last paragraph of https://docs.djangoproject.com/en/dev/ref/models/fields/#field-choices:
So I’d say the answer is no and you’d be better of with a separate Day model with a field for ordering
Even though it feels like overkill for 7 items!