So I am using MPTT for a Category model in Django, and I was wondering if there is a way to filter a Category if there is no child
.
models.py:
class Category(MPTTModel, TimeStampedModel):
title = models.CharField(max_length=75)
parent = TreeForeignKey('self', null=True, blank=True, on_delete=models.SET_NULL, related_name='children', db_index=True)
Categories example in DB:
Games > Nintendo > Nintendo 64
Games > Microsoft > Xbox One
I want to be able to run a command like this:
Category.objects.all().has_no_children()
Hoping that it would return [Nintendo 64, Xbox One]
You are trying to get what is called leaves
. This should help you:
Category.objects.filter(lft=F('rght')-1)