[英]Filtering with Django-Taggit: Is it possible to filter for model entries that include all tags?
The code: 编码:
Food.objects.filter(tags__name__in=["Tag 1","Tag 2","Tag 3"]).distinct()
More precisely, if I want to filter for Food(s) where Food(s) has at least all three tags (not just one or two of the three, but could have more than the three), is there a modification to the filter provided in docs that achieves this result? 更准确地说,如果我要过滤的食物中的食物至少具有所有三个标签(不仅是三个标签中的一个或两个,而且可能具有三个以上标签),是否需要对过滤器进行修改提供达到此结果的文档?
http://django-taggit.readthedocs.io/en/latest/api.html http://django-taggit.readthedocs.io/en/latest/api.html
Try: 尝试:
tag_list = ["Tag 1","Tag 2","Tag 3"]
one way is to use multiple filters in a chained manner. 一种方法是以链接方式使用多个过滤器。 Like this
像这样
Food.objects.filter(tags__name=tag_list[0]).filter(tags__name=tag_list[1]).filter(tags__name=tag_list[3]).distinct()
another way is to use annotation approach 另一种方法是使用注释方法
Food.objects.filter(tags__name__in=tag_list).annotate(num_tags=Count('tags')).filter(num_tags__gte=len(tag_list)).distinct()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.