0

How to double filter with the same field? this is my sample below, i didnt get any data , i just want to filter the Status = 'cut off completed' and Status = 'cut off completed' but i didnt get any data from it.

deliverySchedule = DeliverySchedule.objects.filter(Status = 'cut off completed').filter(Status = 'Active').order_by('deliverySchedule')

my data from django admin

enter image description here

1
  • Why do you want to use double filter? One filter is capable enough to fetch all the results. Commented Oct 6, 2020 at 10:28

3 Answers 3

1
from django.db.models import Q
deliverySchedule = DeliverySchedule.objects.filter(Q(Status = 'cut off completed') | Q(Status = 'Active')).order_by('deliverySchedule')

Based on your explanation I think using this above query will serve the purpose.

Sign up to request clarification or add additional context in comments.

Comments

0

You here filter for Schedule objects where the status is both cut off completed and Active. But the value in a field can not be two different things.

You can make use of Q objects [Django-doc] to specify a logical or:

from django.db.models import Q

deliverySchedule = DeliverySchedule.objects.filter(
    Q(Status='cut off completed') |
    Q(Status='Active')
).order_by('deliverySchedule')

but likely it is more elegant to usee a __in lookup [Django-doc]:

deliverySchedule = DeliverySchedule.objects.filter(
    Status__in=['cut off completed', 'Active']
).order_by('deliverySchedule')

Comments

0

You can filter with __in like so:

deliverySchedule = DeliverySchedule.objects.filter(Status__in=['cut off completed', 'Active']).order_by('deliverySchedule')

If your queryset is empty then there might not be any DeliverySchedule entries with that status.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.