I'm developping a web api using django, I have a TimeField that stores hours:minutes:seconds, since it will be compared to a hours:minutes (without seconds) coming from the mobile app, the comparison will always fail.
here is a comparison without seconds which is returnning an empty QuerySet:
>>> journey = Journey \
... .objects \
... .filter((Q(route__departStation=1) | Q(route__stop__station_id=1)),
... (Q(route__arrivalStation=1) | Q(route__stop__station_id=1)),
... route__departDate="2019-07-31", route__departTime="10:57")
>>> journey
<QuerySet []>
and here is when I add seconds to the comparison:
>>> journey = Journey \
... .objects \
... .filter((Q(route__departStation=1) | Q(route__stop__station_id=1)),
... (Q(route__arrivalStation=1) | Q(route__stop__station_id=1)),
route__departDate="2019-07-31", route__departTime="10:57:05")
>>> journey
<QuerySet [<Journey: Journey object (1)>]>
so please, how can I prevent the seconds from being saved to the database from this TimeField(), or at least how can I limit the comparison to hours and minutes only.
You can use a
TruncMinuteexpression [Django-doc] here:That being said, I suggest you use a
DateTimeFieldover aDateFieldandTimeField. Time actually only makes sense in combination with a date (and timezone). Many countries have daylight saving time (DST), or have changed their timezone throughout history. Therefore it is better to combine these.