Django tagging-tags 如何获取顶部标签的查询集?

发布于 2024-11-16 04:00:34 字数 1497 浏览 3 评论 0原文

我想为一个类编写一个管理器函数,该函数返回模型的关联标签并为每个标签分配一个计数值。

例如:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷爱 2024-11-23 04:00:34

所以这是我发现的一种方法。

基本上我完全忽略视图数据并使用 tagging-tags 模板标签直接访问模型内​​容。

如此

#managers.py
class SnippetManager(models.Manager):
def top_tags(self, klass):
    return Tag.objects.all()

然后……

{% comment %} top_tags.html {% endcomment %}
{% load tagging_tags %}

<html>
    <head>
        <title>Top Tags</title>
</head>
<body>
    {% tags_for_model calloway.Snippet as snippet_tags with counts %}
    {% regroup snippet_tags|dictsort:"count" by count as regrouped_snippet_tags %}
    {% for group in regrouped_snippet_tags reversed %}
        {% for thistag in group.list %}
            <h2>Tag: {{ thistag.name }}</h2>
            <p>Count: {{ group.grouper }}</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 %}
    {% endfor %}
</body>
</html>

尽管最终结果是我想要的,但这似乎是一种笨拙的做法。

有没有办法通过管理器和视图来做到这一点?我认为这就是这次演习的目的。

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

#managers.py
class SnippetManager(models.Manager):
def top_tags(self, klass):
    return Tag.objects.all()

and then...

{% comment %} top_tags.html {% endcomment %}
{% load tagging_tags %}

<html>
    <head>
        <title>Top Tags</title>
</head>
<body>
    {% tags_for_model calloway.Snippet as snippet_tags with counts %}
    {% regroup snippet_tags|dictsort:"count" by count as regrouped_snippet_tags %}
    {% for group in regrouped_snippet_tags reversed %}
        {% for thistag in group.list %}
            <h2>Tag: {{ thistag.name }}</h2>
            <p>Count: {{ group.grouper }}</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 %}
    {% endfor %}
</body>
</html>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文