Search code examples
pythondjangodjango-q

Logical operations in django filter


I need to get my queryset where:

(a="a" AND b=None) OR (a=None AND b="b")

I know about Q objects in django, but this syntax doesn't works:

cls.objects.filter(models.Q(a="a", b=None) | models.Q(a=None, b="b"))

I am absolutely sure my database contains expected objects. But all I get is the empty queryset.
I guess there is some problem with syntax here. But where?
excepted rows picture. i need only first one and second one


Solution

  • cls.objects.filter(models.Q(a="a", b='None') | models.Q(a='None' AND b="b"))
    

    try this