Search code examples
pythondjangodjango-ormdjango-filter

How to exclude two conditions in query via Django ORM?


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".


Solution

  • 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/