How can I exclude by two conditions which are connected via a logical OR:
Object.objects.filter(country_send=country).exclude(status__exact='').order_by('-id')
Object.objects.filter(country_send=country).exclude(status__exact='deleted').order_by('-id')
I'm trying to exclude the object with no status and status "deleted".
The exclude() method from the QuerySet class returns all objects that do not match the given keyword arguments. So whatever data matches the parameter that is passed into the exclude() method will be excluded from the returned QuerySet.
One of the most powerful features of Django is its Object-Relational Mapper (ORM), which enables you to interact with your database, like you would with SQL. In fact, Django's ORM is just a pythonical way to create SQL to query and manipulate your database and get results in a pythonic fashion.
You can try using "lists". On status list you can add all the words you want.
status = ['deleted', '']
Object.objects.filter(country_send=country).exclude(status__in=status).order_by('-id')
More about list: http://www.sthurlow.com/python/lesson06/
Have a look to Q Objects
Your query will be:
from django.db.models import Q
Object.objects.filter(country_send=country).exclude(Q(status__exact='') | Q(status__exact='deleted')).order_by('-id')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With