0
class Order(models.Model):
     date_ordered = models.DateTimeField(auto_now_add = True)

I have date_ordered field inside order model. And now i want to check if the date_ordered and today's date is within 7 days or not inside views.py .

For today's date i have used : current_day = datetime.datetime.now() ( if i need to use another, please suggest)

Can anyone please help me.

1 Answers1

2

You can do this:

from datetime import date, timedelta


current_date = date.today()
future_date = current_date + timedelta(days=7)

orders = Order.objects.filter(
    date_ordered__range=(current_date, future_date),
)  

Warning: Filtering a DateTimeField with dates won’t include items on the last day, because the bounds are interpreted as “0am on the given date”.

Mojtaba Arezoomand
  • 2,140
  • 8
  • 23
  • Hey man , I bumped into some problem . I have this in views.py `orders_completed = Order.objects.filter(customer=customer, complete=True) orders= [] for i in orders_completed : ordered_date = i.date_ordered valid_date = ordered_date + timedelta(days=2) if Order.objects.filter(id=i.id , date_ordered__range=(ordered_date ,valid_date) ): orders.append(i)` and an order was placed in september 18 , but today is 25th sep and my order is still displaying in frontend. Can you please help me in this – Manas Man Singh Sep 25 '21 at 13:17
  • Please ask it in another question. @ManasManSingh – Mojtaba Arezoomand Sep 25 '21 at 13:36
  • i have written another question below , can you please look – Manas Man Singh Sep 25 '21 at 14:54
  • Hey , can you please answer this , i have asked another question for this https://stackoverflow.com/questions/69336951/using-range-in-datetime-in-django – Manas Man Singh Sep 26 '21 at 16:45