[Answer]-Filter results if not contained in another column

1👍

Try

nodetochild.objects.exclude(childid=nodetochild.objects.values_list('Nodeid', flat=True)).only('id', 'childid')

This should evaluate to, more or less:

SELECT "mysite_nodetochild"."id", "mysite_nodetochild"."childid" FROM "mysite_nodetochild" WHERE NOT ("mysite_nodetochild"."childid" =  (SELECT U0."nodeid" FROM "mysite_nodetochild" U0))

Or, if you need the IN condition:

nodetochild.objects.exclude(childid__in=nodetochild.objects.values_list('Nodeid', flat=True)).only('id', 'childid')

Would evaluate to:

SELECT "mysite_nodetochild"."id", "mysite_nodetochild"."childid" FROM "mysite_nodetochild" WHERE NOT ("mysite_nodetochild"."childid" IN  (SELECT U0."nodeid" FROM "mysite_nodetochild" U0))

Leave a comment