Sort Model Objects Based On Time

Hey everyone,

I have a model set up with each object containing a start time field of the form HH:MM AM/PM. I’d like to ideally list the filter call for these objects in ascending order of start time. What’s the best way to do this?

Hopefully you’re not storing the time field in that format in the database. You should be storing it as a TimeField (if the date is really irrelevant); or with a DateField, or as a DateTimeField if it is.
You can then sort by those fields, and display them using the time filter.

Ken

How would I convert that format to a Time Field in Django?

The input I get from my AJAX form gives it in that format, so I’d need to convert it to store it in the aforementioned way @KenWhitesell.

See the TIME_INPUT_FORMATS setting, or more directly, the Python strptime function.

Ken

1 Like

Works perfectly Ken, just tried, thanks a lot.