简体   繁体   English

在django-taggit中,有没有办法将所有标签关联到模型?

[英]In django-taggit, is there a way to get all tags assoicated to a model?

I know how to get all tags, but let's say the tags are connected to lots of different models. 我知道如何获取所有标签,但可以说标签已连接到许多不同的模型。

Somehow I just want to extra the tags that is attached to a model not ALL tags. 我只想以某种方式额外增加附加到模型的标签,而不是所有标签。

Is this possible? 这可能吗?

I know that I can do it in reverse, get the model then get all the tags, but I have LOTS rows in that model and if I try it this way, I have to get all rows then loop through all rows to get queryset of the tags 我知道我可以反向进行操作,获取模型,然后获取所有标签,但是我在该模型中有很多行,如果以这种方式尝试,则必须获取所有行,然后遍历所有行以获取queryset标签

But all I want is get all the tags attached to the model and count the tags. 但是我想要的就是获取所有附加到模型的标签并计数标签。 I do not require to know which row of the model is using this tag. 我不需要知道模型的哪一行正在使用此标记。

Thanks in advance for any help. 在此先感谢您的帮助。

You can directly call tags on Model class itself. 您可以直接在Model类本身上调用标签。 Try with below code 尝试以下代码

tags = YourModel.tags.all()
tags = tags.annotate(tag_count=Count('taggit_taggeditem_items'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM