Django filter by date gives wrong results
I am trying to filter data retrieved from a postgresSQL table using django filter method. Below is the code I am using for that and I am giving "2022-7-13" as the filter_date in the query. The database has 62 entries for the date "2022-7-13" but, the API gives 23 as the result.
I have set USE_TZ = True in my django settings.py file and I am using pendulum.timezone('America/Los_Angeles') as the timezone in django settings file. Can anyone help to resolve this issue.
def get_runs(self, obj):
""" drf serializer method field
"""
runs = 0
date = self.context.get('request').query_params.get('filter_date', None)
if date is not None:
filter_date = datetime.strptime(date, '%Y-%m-%d')\
.replace(tzinfo=pendulum.timezone('America/Los_Angeles')).date()
runs = obj.runs.filter(created_on__date=filter_date).count()
else:
runs = obj.runs.count()
return runs
Can you please explain, what you are getting in date and what is its datatype?