python - Django: Query which excludes all children if the parent is included in the query -


class person(timestampedmodel):     name = models.charfield(max_length=32)     parent = models.foreignkey('self', null=true, blank=true, related_name='children')     is_child = models.booleanfield(default=false,) 

i trying form query excludes children if parent present. children, represent 30% of model's entries -for time being- in postgresql.

my approach use nested query. however, not sure efficient solution.

i appreciate help.

update

the python solution came following:

a = person.objects.filter(...) ids = [i.id in a] result = [x x in if any((not x.is_child, x.parent_id not in ids))] 

try this:

qs0 = person.objects.filter(...) qs = qs0.exclude(is_child=true, parent__in=qs0) 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -