Django date range from today and 10 days ago

I have a model:

title = models.CharField(max_length=120)
description = models.TextField()
post_date = models.DateTimeField(default=timezone.now)
published = models.BooleanField(default=False)
vote = models.IntegerField(default=0)  

      

Here I want to display questions posted within 10 days, for example

Question.objects.filter(post_date__range=(today, 10 days back))

      

How can i do this?

+3


source to share


1 answer


import datetime
today = datetime.today()
Question.objects.filter(
    post_date__range=(today-datetime.timedelta(days=10), today)
)

      

something like this should work.



You can do adding / faking time to date with timedelta

.

+2


source







All Articles