开发者

Django tagging-tags how to get queryset of top tags?

开发者 https://www.devze.com 2023-03-13 02:44 出处:网络
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.

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.p开发者_运维知识库y:
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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消