[英]Annotate django-taggit tags attached to specific object
I have an object that is tagged using django-taggit . 我有一个使用django-taggit标记的对象。 If I wanted to get a list of all the tags attached to this object, I would follow the documentation like so:
如果我想获取附加到该对象的所有标签的列表,请遵循以下文档:
apple = Food.objects.create(name="apple")
apple.tags.add("red", "green", "delicious")
apple.tags.all()
If I wanted to know how many Food objects were attached to each tag in existence, I would do the following: 如果我想知道每个标签上附加了多少Food对象,请执行以下操作:
Tag.objects.all().annotate(food_count=Count('food'))
If I wanted to get a count of all of the food items attached only to the tags that are attached to 'apple', I could do the following: 如果我想获取仅附在“苹果”标签上的所有食品的计数,我可以执行以下操作:
apple = Food.objects.create(name="apple")
apple.tags.add("red", "green", "delicious")
apple.tags.all().annotate(food_count=Count('food'))
Ok, so for my question. 好的,我的问题是。 Let's say my Food model has a field with a flag:
假设我的食物模型有一个带有标志的字段:
class Food(models.Model):
name = models.CharField(max_length=200, unique = True)
healthy_flag = models.BooleanField(default=False)
How can I get a count of all of the healthy foods attached only to the tags that are attached to 'apple' (where a healthy food is denoted by healthy_flag = 1)? 如何获得仅贴在“苹果”上的标签上附加的所有健康食品的计数(其中健康食品用Healthy_flag = 1表示)? Basically, for every 'apple' tag, how many healthy foods share that tag?
基本上,对于每个“苹果”标签,有多少健康食品共享该标签?
在这里找到答案。
apple.tags.all().annotate(food_count=Count('food')).filter(food__health_flag = True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.