Django MPTT 获取特定的子类别

问题描述

我有两个模型一个类别和一个产品模型,类别使用django MPTT。我无法获取或过滤特定的孩子。

class Item(models.Model):
    title = models.CharField(max_length=150)
    merchant = models.ForeignKey(
        settings.AUTH_USER_MODEL,verbose_name="Merchants",on_delete=models.CASCADE,related_name="items"
    )
    category = models.ForeignKey(
        Category,related_name="category"
    )
    price = models.DecimalField(max_digits=9,decimal_places=2)
    discount_price = models.DecimalField(
        max_digits=9,decimal_places=2,blank=True,null=True
    )
class Category(MPTTModel):
    title = models.CharField(max_length=100)
    parent_category = TreeForeignKey(
        "self",null=True,related_name="sub_categories",)
    slug = models.SlugField(max_length=100)
    vertical = models.ForeignKey(Vertical,null=True)

    class Meta:
        indexes = [GinIndex(fields=["title","parent_category"])]
        verbose_name_plural = "categories"
        unique_together = ["slug","parent_category"]

    class MPTTMeta:
        order_insertion_by = ['title']
        parent_attr = 'parent_category'

当我针对 sub_category 运行查询集时遇到的问题,它继续为我提供主类别中的所有对象

items = Item.objects.filter(category__parent_category__sub_categories__slug='flu')

但是它返回父类别下的所有项目,而不仅仅是属于流感类别的项目

非常感谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...