Django-Tagging - 对顶级“标签”进行计数和排序(有更清洁的解决方案吗?)

发布于 2024-08-14 17:53:43 字数 551 浏览 5 评论 0原文

我正在使用 Django-Tagging,而且我并不完全需要云,我只想要我的博客条目中使用的最流行标签的有限列表。

使用以下内容:

[(tag.name, int(tag.count)) for tag in Tag.objects.usage_for_model(Post, counts=True)]

它返回一个数组(请注意,我在开发时使用 Lorem Ipsum):

[(u'deposit', 5), (u'escorol', 1), (u'gratuitous', 8), (u'marquee', 2)]

但是为了排序和限制它,我需要这样做:

sorted([(tag.name, int(tag.count)) for tag in Tag.objects.usage_for_model(Post, counts=True)], key=lambda k:k[1], reverse=True)[:10]

有没有更简洁的方法来做到这一点?我感觉一定有。

I'm using Django-Tagging, and I don't exactly need a cloud, I just want a limited list of the most popular tags used in my blog entries.

Using the following:

[(tag.name, int(tag.count)) for tag in Tag.objects.usage_for_model(Post, counts=True)]

It returns an array (note I'm using Lorem Ipsum while I develop):

[(u'deposit', 5), (u'escorol', 1), (u'gratuitous', 8), (u'marquee', 2)]

But then to order and limit it I need to then do this:

sorted([(tag.name, int(tag.count)) for tag in Tag.objects.usage_for_model(Post, counts=True)], key=lambda k:k[1], reverse=True)[:10]

Is there a neater way to do this? I feel like there must be.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

哥,最终变帅啦 2024-08-21 17:53:43

如果您使用的是最新版本的 django,则可以使用聚合。 http://docs.djangoproject.com/en/dev/topics/db/aggregation 该页面上的一个示例..

Book.objects.annotate(num_authors=Count('authors')).order_by('num_authors')

If you are using the latest version of django, you could use aggregation. http://docs.djangoproject.com/en/dev/topics/db/aggregation an example on that page ..

Book.objects.annotate(num_authors=Count('authors')).order_by('num_authors')
屋檐 2024-08-21 17:53:43

Django 的 {% regroup %} 模板标签对此可能有用。假设 tags 位于模板的上下文中:

{% regroup tags|dictsort:"count" by count as sorted_tags %}
...
{% for count in sorted_tags %}
...
    {% for tag in count %}
    ...
    {% endfor %}
{% endfor %}

Django's {% regroup %} template tag might be useful for this. Assuming tags is in the context of your template:

{% regroup tags|dictsort:"count" by count as sorted_tags %}
...
{% for count in sorted_tags %}
...
    {% for tag in count %}
    ...
    {% endfor %}
{% endfor %}
悲欢浪云 2024-08-21 17:53:43

我发现下面的排序代码比您编写的代码更具可读性。当然,这并不能消除 abeyer 所说的源问题

import operator
tags = Tag.objects.usage_for_model(Post, counts=True)
tags.sort(key=operator.attrgetter('count'), reverse=True)

I find the following sorting code a bit more readable than the one you wrote. Of course it doesn't remove the source problem stated by abeyer

import operator
tags = Tag.objects.usage_for_model(Post, counts=True)
tags.sort(key=operator.attrgetter('count'), reverse=True)
初相遇 2024-08-21 17:53:43

如果您无论如何都需要提取所有标签,并且对前 n 个标签的限制只是一个演示问题,那么 Fragsworth 的答案可能是正确的选择。如果您没有在其他地方使用其余标签,我认为这确实应该在数据库查询中发生......您可能希望在以下位置执行“ORDER BY count DESC LIMIT n”查询以避免拉出一堆您不会使用的标签。

然而,django-tagging 似乎被硬编码为始终按标签 ID 和名称进行分组/排序。我想说正确的解决方案是针对该问题提交一个错误,并公开一个 api,该 API 将为您返回前 n 个标签。

If you need to pull all the tags anyway, and the limit to the top n is simply a presentation thing, Fragsworth's answer is probably the way to go. If you aren't using the remainder of the tags somewhere else, I'd argue that this is really something that should be happening in the database query...you would want to be doing an 'ORDER BY count DESC LIMIT n' on the query to avoid pulling a bunch of tags that you aren't going to use.

However, it appears that django-tagging is hard coded to always group/sort by tag ids and names. I'd say the right solution is to file a bug against that, and get an api exposed that will give you back the top n tags.

饮惑 2024-08-21 17:53:43

我为此使用原始 sql:

trends = Tag.objects.raw("select tagging_tag.id, count(object_id) as counter from tagging_tag left join tagging_taggeditem on tagging_tag.id = tagging_taggeditem.tag_id group by tagging_tag.id order by counter DESC limit 10")

I use raw sql for this:

trends = Tag.objects.raw("select tagging_tag.id, count(object_id) as counter from tagging_tag left join tagging_taggeditem on tagging_tag.id = tagging_taggeditem.tag_id group by tagging_tag.id order by counter DESC limit 10")
时光病人 2024-08-21 17:53:43

我在 Django-Tagging 中获取顶级标签的方法是:

top_tags = Tag.objects.annotate(num=Count('taggit_taggeditem_items')).order_by('-num')

My approach for getting top tags in Django-Tagging is:

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