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
Post a Comment