I have a Django model with separate Datefield and Timefield for an event. Is there a way to convert it to a python datetime object so I can query for the upcoming events with some precision? Currently I get only the upcoming of the following day.
models.py
event_time = models.TimeField()
event_date = models.DateField()
Basically, can I filter with a minute, or even split second precition?
Thank you.
Use a
DateTimeFieldinstead (see this section in the docs). Conversion to adatetime.datetimeis handled for you by Django automatically.A
DateFieldresults in adatetime.dateand adatetime.timeobject. You can usereplaceto merge these values into an updateddate:Note that the resulting
datehas 11.30 as time now. Note also thattodayis not modified, it simply computes a new date and time. As you can see, you now have to do the merging yourself because both values are stored in separate fields. That’s why aDateTimeFieldis a much better choice, if you have the ability to modify the model’s fields.