0

I bumped into some problem . I have this in models.py

class Order(models.Model):
     customer = models.ForeignKey('Customer' , on_delete=models.SET_NULL 
 , null= True , blank = True)
     date_ordered = models.DateTimeField(auto_now_add = True)
     complete = models.BooleanField(default=False)

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=5)         
    if Order.objects.filter(id=i.id , date_ordered__range=(ordered_date 
    ,valid_date)):
     orders.append(i)

And I have sent orders in context for frontend.

    context = {'orders': orders , 'cartItems' : cartItems 
    ,'returns':returns}

After this , even if the order places was on september 19th , its still showing when today is 28 th of September. I have specified the valid date as +5 of ordered date.

  • Uhh, `ordered_date = i.date_ordered` and then you literally check if `date_ordered` is in between `ordered_date` and `ordered_date + timedelta(days=5)` of course it will be true. Also even though you have the object in memory you make a _database query_ to then check that condition... What exactly do you want to do? – Abdul Aziz Barkat Sep 26 '21 at 16:48
  • Could you describe in English what you aims to do. Then provide minimal input model and an example of desired output. – jlandercy Sep 26 '21 at 16:54
  • @AbdulAzizBarkat , i.date_ordered means , i have ordered date in my Order model , and i want the data of Order.objects only between date_ordered + ( date_ordered + valid date) .... i think i didn't understand the meaning of __range in django ... could you please explain me with this – Manas Man Singh Oct 10 '21 at 13:43
  • @jlandercy could you please check the question again , i have added my models code also – Manas Man Singh Oct 10 '21 at 13:47
  • This is totally normal because you ask your order to be in range containing the order_date. So all records will always makes this criterion true. We need to understand what you want to achieve here. But we have no clue on it, you must describe what you aim to achieve, eg. I want records with order date older than 5 days since today. Then it will be possible to help you. – jlandercy Oct 10 '21 at 14:22
  • @jlandercy , yes bro , i was so dumb , i was not getting the __ranage method . Can you please help me in eg. I want records with order date older than 5 days since today this please – Manas Man Singh Oct 10 '21 at 14:53

1 Answers1

0

If the question you want to answer implies to know when we are then you will definitely need to use current time in your query maybe with some additional arithmetic in the filter clause.

If you meant order placed the last five days until now, the query will looks like:

from django.utils import timezone

now = timezone.now()
last5days = now - timedelta(days=5)

Order.objects.filter(customer=customer, complete=True, date_ordered__gte=last5days)

If you meant order older than 5 days, it will looks like:

Order.objects.filter(customer=customer, complete=True, date_ordered__lt=last5days)

Nota: You really don't want to add this extra loop which ruins performance (submit many queries instead of a single one, add extra unnecessary variables) and readability. Just chain you query in a proper way.

jlandercy
  • 7,183
  • 1
  • 39
  • 57
  • @ManasManSingh Does this answer solve your issue? – jlandercy Oct 12 '21 at 17:00
  • hey , thank for your answer , but i have solved using __range as i got help from another person .Thank you again. – Manas Man Singh Oct 22 '21 at 12:22
  • @ManasManSingh, Well I am glad you have found a solution. But your post really miss the big point here. It is not just about helping someone in specific but more having a community approach. Would you mind take 5 min to assess my answer and cast your vote if it is useful and actually address your question. You may also publish your answer this question and then share your solution. That would be fair and community oriented – jlandercy Oct 22 '21 at 15:59