在Django的Queryset中过滤django-taggit的标签 [英] filter tags of django-taggit in Django's Queryset

查看:138
本文介绍了在Django的Queryset中过滤django-taggit的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

拥有以下模型:

  class Post(models.Model):
title = models.CharField max_length = 250)
tags = TaggableManager()

,数据为:

  ** post.title ** ** post.tags ** 
Django通过示例python,django,web
谁是Django Reinhardt python,django,
使用Python进行测试驱动开发python,web
数据分析Python Python数据
学习Python python
编程Python python
使用Python自动化无聊的东西python

我尝试下面的代码

 >>> alist = Post.objects.filter(tags__name__in = [data,python])
>>>因为我在alist.annotate(sam_tags = Count('tags')):
... print(\\\
---,i)
... print( :,i.tags.all())
... print(sam_tags的值,i.sam_tags)
...

--- Django通过示例
标签:[<标签:django><标签:python><标签:web>]
sam_tags的值:1

- - 谁是Django Reinhardt
的标签:[<标签:django><标签:python>]
sam_tags的值:1

---自动化Python的
标签:[<标签:python>]
sam_tags的值:$ 1

---使用Python的测试驱动开发
标签:[<标签:python><标签:web>]
sam_tags的值:1

---学习Python
标签: [< Tag:python>]
sam_tags的值:1

---用于数据分析的Python
标签:[< Tag:data>标签:python>]
sam_tags的值:2

---编程Python
t它的标签:[<标签:python>]
sam_tags的值:1
>>>

为什么slist [0] .sam_tags(post:Django By Example)的值等于1



我认为(post:Django By Example)的post对象在阅读Django的文档后有三个标签[python,django和web]。



https:// docs。 djangoproject.com/en/1.10/ref/models/querysets/#count



它表示 Count(expression)返回通过提供的表达式相关的对象的数量。
所以代码

 >>> alist [0] .tags.all()
[<标签:django><标签:python><标签:web>]

显示alist [0] .tags中有三个标签,

 >>> slist = alist.annotate(sam_tags = Count('tags'))
>>> slist [0] .tags.all()
[< Tag:django><标签:python><标签:web>]
>>> slist [0] .sam_tags
1

但这里我得到的值1, >
为什么?



我知道Django只计算我的filter子句中包含的python和data标签 - 其他标签不是



slist [0] .tags.all()的输出显示slist [0]有三个与自身相关的标签。因为django的文档表示Count(表达式)返回通过提供的表达式相关的对象的数量,slist [0] .sam_tags应该是3根据文档,但是django-taggit使slist [0] .sam_tags那么我真正想知道的是如何使用django-taggit让filter子句中的Count(表达式)只计算过滤条件中的标签数量。

解决方案

Django只计算 python 数据您包含在过滤器子句中的标签 - 其他标记不计入。 (请注意, sam_tags 为2的唯一示例是标记为数据 python 。)这可能是意想不到的行为,但如果您考虑如何执行底层SQL,这是有道理的。请参阅与您的类似模式的示例:

 >>> a = Article.objects.filter(tags__slug__in = ['python'])。annotate(num_tags = Count('tags'))[0] 
>>> a.num_tags
1
>>>> a.tags.count()
2

如果我将filter子句更改为过滤除了标签之外,它的行为与预期的一样:

 >>> Article.objects.filter(pk = a.pk).annotate(num_tags = Count('tags'))[0] .num_tags 
2
>>> Article.objects.filter(pk = a.pk).annotate(num_tags = Count('tags'))[0] .tags.count()
2
/ pre>

Having the following models:

class Post(models.Model):
    title = models.CharField(max_length=250)
    tags = TaggableManager()

and the data are:

**post.title**                          **post.tags**
Django By Example                       python,django,web
Who was Django Reinhardt                python,django,
Test-Driven Development with Python     python,web
Python for Data Analysis                python,data         
Learning Python                         python
Programming Python                      python
Automate the Boring Stuff with Python   python

I try to code below

>>> alist=Post.objects.filter(tags__name__in=["data","python"])
>>> for i in alist.annotate(sam_tags=Count('tags')):
...     print("\n---",i)
...     print("tags of it : ",i.tags.all())
...     print("value of sam_tags : ",i.sam_tags)
... 

--- Django By Example
tags of it :  [<Tag: django>, <Tag: python>, <Tag: web>]
value of sam_tags :  1

--- Who was Django Reinhardt
tags of it :  [<Tag: django>, <Tag: python>]
value of sam_tags :  1

--- Automate the Boring Stuff with Python
tags of it :  [<Tag: python>]
value of sam_tags :  1

--- Test-Driven Development with Python
tags of it :  [<Tag: python>, <Tag: web>]
value of sam_tags :  1

--- Learning Python
tags of it :  [<Tag: python>]
value of sam_tags :  1

--- Python for Data Analysis
tags of it :  [<Tag: data>, <Tag: python>]
value of sam_tags :  2

--- Programming Python
tags of it :  [<Tag: python>]
value of sam_tags :  1
>>> 

why is the value of slist[0].sam_tags (post:Django By Example) equal to 1?

I think the post object of (post:Django By Example) has three tags [python,django and web] after reading the Django's documentation.

https://docs.djangoproject.com/en/1.10/ref/models/querysets/#count

It said that Count(expression) returns the number of objects that are related through the provided expression. so the code

>>>alist[0].tags.all() 
[<Tag: django>, <Tag: python>, <Tag: web>]

shows there are three tags in alist[0].tags,

>>> slist=alist.annotate(sam_tags=Count('tags'))
>>> slist[0].tags.all()
[<Tag: django>, <Tag: python>, <Tag: web>]
>>> slist[0].sam_tags
1

but here I get the value 1,
why?

I understand that Django is counting only the "python" and "data" tags which I included in my filter clause - other tags are not counted.

The output of "slist[0].tags.all()" shows that slist[0] has three tags related with itself. Because the documentation of django says that Count(expression) returns the number of objects that are related through the provided expression, slist[0].sam_tags should be 3 according to the documentation, but django-taggit make slist[0].sam_tags to be 1.

So what I really want to know is how django-taggit lets Count(expression) in the filter clause only calculate the number of tags in the filter condition .

解决方案

Django is counting only the python and data tags which you included in your filter clause - other tags are not counted. (Note that the only example with sam_tags of 2 is the one tagged both data and python.) This is probably unexpected behavior, but makes sense if you consider how the underlying SQL is executed. See this example from a similar schema to yours:

>>> a = Article.objects.filter(tags__slug__in=['python']).annotate(num_tags=Count('tags'))[0]
>>> a.num_tags
1
>>> a.tags.count()
2

If I change the filter clause to filter on somethign other than tags, it behaves as expected:

>>> Article.objects.filter(pk=a.pk).annotate(num_tags=Count('tags'))[0].num_tags
2
>>> Article.objects.filter(pk=a.pk).annotate(num_tags=Count('tags'))[0].tags.count()
2

这篇关于在Django的Queryset中过滤django-taggit的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆