[Fixed]-Django MPTT queryset for instances with children with a certain attribute

1👍

Well, the pure property of being a parent is filtered for via the __isnull filter. This generally holds for reverse foreign key testing:

Foobar.objects.filter(children__isnull=False)  # must have children

It should be mentioned that 'children' in this query is the related_query_name of the ForeignKey. This defaults to the related_name if that is provided or to the lowercase model name (foobar in this case).

With a concrete child property, you can simply do:

Foobar.objects.filter(children__published=True)

This will only include Foobars with at least one child that is published.

Leave a comment