Something fails to make a filter in python. The idea is this: I need to select all the objects that have not a single descendant, and those whose descendants have a score of zero. I can write two expressions separately, but I did not find how to write them together. I will be glad to answer, and even more reference to man.

Objects without descendants

objects.filter(children__isnull=True) 

Objects with descendants of zero points

 objects.filter(children__point=0) 

    2 answers 2

    It is enough to write this:

     objects.filter(children__isnull=True, children__point=0) 

    UPD: I understood the question as intersection (AND) of requests. If a union (OR) is required, you need to use the Q object.

    • THX. The question, as it turned out, was wildly simple, but it was just impossible to find it, if not Google, it isn’t in the Russian Internet like this :(. - Nicolas Chabanovsky

    If I get it right, do it like this:

     from django.db.models import Q ...filter(Q(children__isnull=True) | Q(children__point=0)) 

    Generally read this

    • There is a lot of filtering on the link, read the official docks :) - metazet
    • Already in the process :). The link is cool. - Nicolas Chabanovsky