简体   繁体   English

仅当不存在子代时,Django MPTT过滤器

[英]Django MPTT Filter Only if No Children Exist

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 . 因此,我在Django中将MPTT用于类别模型,并且我想知道是否有一种方法可以在没有child的情况下过滤类别。

models.py: 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: 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] 希望它会返回[Nintendo 64, Xbox One]

You are trying to get what is called leaves . 您正在尝试获取所谓的leaves This should help you: 这应该可以帮助您:

Category.objects.filter(lft=F('rght')-1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM