[Answered ]-Django: how select items that do not have referencies from other items?

1πŸ‘

βœ…

I think it can be done with annotate and exact django’s directives.

It can be done even simpler. You can filter with:

Critters.objects.filter(parent=None, critters=None)

We thus filter on the reversed relation. This is a LEFT OUTER JOIN and then check if that is None. It will thus make a query that looks like:

SELECT *
FROM critters
LEFT OUTER JOIN critters AS c ON c.parent_id = critters.id
WHERE parent_id IS NULL AND c.id IS NULL

0πŸ‘

Kinda confused what you trying to do.
If want to select all value where parent not exist, you can achieve it by doing this:

Critters.objects.filter(parent=None)

There also .exclude you can use.

πŸ‘€GoodDay

Leave a comment