Django tagging-tags 如何获取顶部标签的查询集?
我想为一个类编写一个管理器函数,该函数返回模型的关联标签并为每个标签分配一个计数值。
例如:
#models.py
class Snippet(models.Model):
...
tags = TagField()
objects = managers.SnippetManager()
-------------
#managers.py:
from tagging.models import Tag
class SnippetManager(models.Manager):
def top_tags(self, klass):
tag_list = Tag.objects.usage_for_model(klass, counts=True)
return ???
--------------
#views.py:
from django.views.generic.list_detail import object_list
from calloway.models import Snippet
def top_tags(request):
return object_list(request, queryset=Snippet.objects.top_tags(Snippet),
template_name='calloway/top_tags.html',
paginate_by=20)
我希望 top_tags 管理器返回按附加属性排序的查询集,以便我可以循环遍历 object_list 并选出计数值。
即我的模板如下所示:
{% comment %} top_tags.html {% endcomment %}
{% for thistag in object_list %}
<h2>Tag: {{ thistag.name }}</h2>
<p>Count: {{ thistag.count }}</p>
<p>Snippets:</p>
{% tagged_objects thistag in calloway.Snippet as tagged_snippets %}
{% for tagged_snippet in tagged_snippets %}
<p><a href="{{ tagged_snippet.get_absolute_url }}">{{ tagged_snippet.title }}</a></p>
{% endfor %}
{% endfor %}
任何人都可以推荐一种方法来执行此操作吗?如何将计数绑定到标签? “注释”是解决方案吗?
对于那些感兴趣的人,我正在尝试完成实用 Django 项目第 8 章的“展望未来”段落中的“挑战”。
I want to write a manager function for a class that returns the associated tags for a model and assigns a count value to each tag.
For example:
#models.py
class Snippet(models.Model):
...
tags = TagField()
objects = managers.SnippetManager()
-------------
#managers.py:
from tagging.models import Tag
class SnippetManager(models.Manager):
def top_tags(self, klass):
tag_list = Tag.objects.usage_for_model(klass, counts=True)
return ???
--------------
#views.py:
from django.views.generic.list_detail import object_list
from calloway.models import Snippet
def top_tags(request):
return object_list(request, queryset=Snippet.objects.top_tags(Snippet),
template_name='calloway/top_tags.html',
paginate_by=20)
I'd like the top_tags manager to return a queryset ordered by an appended attribute, so that I can loop over the object_list and pick out the count value.
Ie my template looks like:
{% comment %} top_tags.html {% endcomment %}
{% for thistag in object_list %}
<h2>Tag: {{ thistag.name }}</h2>
<p>Count: {{ thistag.count }}</p>
<p>Snippets:</p>
{% tagged_objects thistag in calloway.Snippet as tagged_snippets %}
{% for tagged_snippet in tagged_snippets %}
<p><a href="{{ tagged_snippet.get_absolute_url }}">{{ tagged_snippet.title }}</a></p>
{% endfor %}
{% endfor %}
Can anyone recommend a way to do this? How can I bind the count to the tag? Is "annotate" the solution?
For those interested I'm trying to complete the "challenge" in the "Looking Ahead" paragraph of Practical Django Projects, Chapter 8.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以这是我发现的一种方法。
基本上我完全忽略视图数据并使用 tagging-tags 模板标签直接访问模型内容。
如此
然后……
尽管最终结果是我想要的,但这似乎是一种笨拙的做法。
有没有办法通过管理器和视图来做到这一点?我认为这就是这次演习的目的。
So here's a way I've found to do it.
Basically I'm ignoring the view data completely and using the tagging-tags template tags to directly access the model contents.
So
and then...
This seems like a cludgy way of doing it though, although the end result is what I want.
Is there a way to do it through the manager and view? I think that's what the exercise is intended to produce.