I'm trying to make a proper date handling setup for my web app. I have a model that looks something like this
class Entity(models.Model):
name = models.CharField(max_length=255)
date = models.DateTimeField()
User can send a request to my DRF endpoint /api/v1/entity/
to get a list of such entities. Now there is a requirement that user should be able to request all Entity
objects for a single day, which is determined by the date parameter. Dates are stored in UTC in the database while users of this app are not in a UTC timezone.
User can create an entity with the following date 2018-06-19T01:00:00+02:00
, which is stored as 2018-06-18T23:00:00Z
in the database. Now if I try to list all entities user has created for 2018-06-19
nothing's returned, but filtering by 2018-06-18
returns one entry.
This is the code setup I'm using:
http://127.0.0.1:8000/api/v1/entity/?date=2018-06-18
.
def get_queryset(self):
user = self.request.user
entities = Entity.objects.filter(owner=user)
date = self.request.query_params.get('date')
if date:
entities = entities.filter(date__date=date)
return entities
So in this case the appropriate date range would be 2018-06-18T23:00:00Z
- 2018-06-19T23:00:00Z
. What's the correct approach to fetch all entities for a single day (or a date range) in user's timezone?