How can I search for time with time zone if I store time by utc
For the purpose of the example I have inserted datetime ‘2021-06-19T16:20:00+01:00’
It’s saved as ‘2021-06-19T15:20:00’
And if I want to search for him I have to search MeteorologicalData.objects.filter(datetime__time=“15:20:00”)
but if i want to search as MeteorologicalData.objects.filter(datetime__time=“16:20:00+01:00”) It does not return anything
What i can do that he can search for me?
Rather than passing the value to the query as a string, convert it to a datetime (or time) object and use that as a parameter.
See the example in the Chaining filters section of the docs to get an idea of how to do this.
If I understand correctly it still requires me to change the time to utc in the query?
To solve it I did that
def get_or_create(self, *args, **kwargs):
kwargs["datetime"] = dt.strptime(kwargs["datetime"], r"%Y-%m-%dT%H:%M:%S%z")
kwargs["datetime"] = kwargs["datetime"].astimezone(pytz.utc)
return super().get_or_create(*args, **kwargs)
But I’m trying to find a more “django solution”
What do you mean by a more “django solution”? Django just provides an abstraction layer on top of the database. It’s the database performing these comparisons, and so you need to supply query data that the database can understand and use.