gpt4 book ai didi

mysql - 注释附加到特定对象的 django-taggit 标签

转载 作者:行者123 更新时间:2023-11-29 00:41:32 25 4
gpt4 key购买 nike

我有一个使用 django-taggit 标记的对象.如果我想获得附加到该对象的所有标签的列表,我会像这样遵循文档:

apple = Food.objects.create(name="apple")
apple.tags.add("red", "green", "delicious")
apple.tags.all()

如果我想知道每个标签附加了多少个 Food 对象,我会执行以下操作:

Tag.objects.all().annotate(food_count=Count('food'))

如果我想统计附加到“apple”标签的所有食品,我可以执行以下操作:

apple = Food.objects.create(name="apple")
apple.tags.add("red", "green", "delicious")
apple.tags.all().annotate(food_count=Count('food'))

好的,我的问题来了。假设我的 Food 模型有一个带有标志的字段:

class Food(models.Model):
name = models.CharField(max_length=200, unique = True)
healthy_flag = models.BooleanField(default=False)

我怎样才能得到所有健康食品的数量附加到“苹果”的标签(健康食品由healthy_flag表示) = 1)?基本上,对于每个“苹果”标签,有多少健康食品共享该标签?

最佳答案

找到答案 here .

apple.tags.all().annotate(food_count=Count('food')).filter(food__health_flag = True)

关于mysql - 注释附加到特定对象的 django-taggit 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12083342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com