[Fixed]-Why date range return empty query?

1👍

Thank to @alfredo138923 for is help.

I post the solution with explication for anyone who need it a day maybe.

page = 0
n = now()
startDate = n-timedelta(days=5*page)     #more recently date
endDate = n-timedelta(days=5*(page+1))   #more older date
Episode.objects.filter(date__range=[endDate, startDate])   #range of 5 days

I fact when you need to filter date you use __range but the more recently date need to be in the seconde position and oldly date in the first.

page = 0
n = now()
Episode.objects.filter(date__range=[n-timedelta(days=(5*(page+1))), n-timedelta(days=(5*page))])

Same code but in one line/query.

Citation of the commentary for anyone who doesn’t read it :

__range is calling sql BETWEEN function for which first argument is greater or equal and second one is less or equal. – @iklinac

0👍

If your page = 0

startDate = n  (as page = 0)
endDate = n - 5 days

which translates into query that searches between start date now and end date 5 days before getting 0 results

range documentation

Leave a comment