[Fixed]-Django filter between two date

21👍

Change the ‘recordDate’ to DateField and use the following query:

asdf.objects.filter(recordDate__gte='2019-03-01', recordDate__lte='2019-03-09')

7👍

In order to get between range use filter this query:

models.asdf.objects.filter(recordDate__gte='start_date', recordDate__lt='end_date')

start_date and end_date may be string in date formats or datetime parsed object.

5👍

This is working for me
remember one thing what you are storing in you db means only date or datetime

when datetime

start = '2021-08-12 21:52:33.118649'
end = '2021-08-13 06:30:46.909572'
user = UserAccount.objects.filter(created_at__gte=start, 
created_at__lte=end)
print user

when date

start = '2021-08-12'
end = '2021-08-13'
user = UserAccount.objects.filter(created_at__gte=start, 
created_at__lte=end)
print user

or you can filter by using __range here also

start = '2021-08-12 21:52:33.118649'
end = '2021-08-13 06:30:46.909572'
user = UserAccount.objects.filter(created_at__range=(start, end))
print user

if you you are storing in db datatime and you want to retrieve only date from db

from datetime import datetime
datetime.strptime('2014-12-04', '%Y-%m-%d').date()

Leave a comment