Djang中从queryset中删除对象的最简单方法

2024-09-26 04:55:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个查询集categories = unipart.categories.all(),它是unipart对象链接到的所有类别。你知道吗

但是,我想从这个列表中删除顶级类别——也就是那些具有unipart也在其中列出的子类别的类别。你知道吗

或者父类id等于查询集中某个类别id的类别。你知道吗

例如,如果unipart列在: 肉豆蔻(母体=香料)和 香料(母体=食物)

然后我只想包括肉豆蔻——所以基本上我想从queryset中“弹出”香料。你知道吗

最好的方法是什么?我宁愿不用单子。你知道吗

以下是我的模型:

class UniPart (models.Model):
categories=models.ManyToManyField(Category, related_name = 'unipart')

class Category (MPTTModel):
    category = models.CharField(max_length=250)
    oc_id= models.IntegerField()
    parent = TreeForeignKey('self', blank=True, null=True, related_name='children')
    def __unicode__(self):

Tags: nameselfidtruemodelsall类别class