如何过滤django-taggit顶部标签 [英] How to filter django-taggit top tags

查看:162
本文介绍了如何过滤django-taggit顶部标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个数据库,其中的User对象在Djano应用程序后运行 并且您想使用django-taggit标记用户对象,以便可以使用一些方便的过滤来检索子组.

Suppose you have a database with User objects running behind a Djano app and you want to use django-taggit to tag User objects so you can retrieve subgroups using some convenient filtering.

此外,您还有一个仪表板,您想在其中显示有关已使用标签的有趣统计信息,以收集有关用户中存在的子组的一些信息.

Additionally you have a Dashboard where you want to display interesting statistics about used tags to glean some information about the subgroups that exists within your Users.

  1. 您将如何访问和显示有关前X个标签的信息 在Django应用中使用了?

  1. How would you access and display information about the top X tags used within the Django app?

如何仅访问已过滤的前X个标记 用户对象的子组?

How would you access only the top X tags of an already filtered subgroup of the User object?

推荐答案

尽管SO上已经有许多帖子描述了类似的问题,但大多数都描述了变通方法或包含分散的信息.

While there are already a number of posts on SO that describe similar problems most of these describe workarounds or contain scattered information.

为了使这些信息更容易找到,我将发布一个简单的摘要,介绍如何使用django-taggit的功能(已得到官方支持,但 )实现一些基本知识strong>出现在官方文档中.

In order to make this information easier to find I will post a simple rundown of how to achieve some basic stuff using features of django-taggit that are officially supported but are not present in the official documentation.

您将如何访问和显示有关前X个标签的信息 在Django应用中使用了?

How would you access and display information about the top X tags used within the Django app?

为了访问和显示有关Django应用程序中使用的顶级标签的信息,您可以使用内置函数most_common,如下所示:

In order to access and display information about the top tags used within the Django app you can use the built in function most_common like so:

top_tags = User.tag.most_common()

这将返回一个查询集,其中包含放置在User实例上的所有标记(按从最常用的降序顺序排列). 假设我们有3个标签:["vegetables", "fruits", "candy"]和10个用户具有fruits标签,4个用户具有vegetables标签,只有1个用户具有candy标签,返回的顺序为:["fruits", "vegetables", "candy"]

This returns a queryset containing all of the tags placed on a User instance ordered from most used descending order. So say we have 3 tags: ["vegetables", "fruits", "candy"] and 10 users have a fruits tag, 4 users have a vegetables tag and only 1 user has the candy tag the returned order would be: ["fruits", "vegetables", "candy"]

可以像这样访问有关返回的标签的更多信息:

Accessing more information about the tags returned can be done like so:

for tag in top_tags:
    print(tag.name) #the name of the tag
    print(tag.num_times) # the number of User objects tagged

此外,如果您仅对前3个标签感兴趣,则可以 像这样访问它们:

Additionally if you are only interested in the top 3 tags then you can access them like this:

top_tags = User.tag.most_common()[:3]

您可以用X代替3,其中X是您要退回的商品数量.

Where you can replace 3 with X where X is the number of items you want returned.

您将如何仅访问已过滤的前X个标记 用户对象的子组?

How would you access only the top X tags of an already filtered subgroup of the User object?

自2016年7月12日起,most_common()函数实际上具有一些您可以指定的附加参数.首先,您可以指定min_count,以过滤掉低于特定阈值的顶部标签.作为示例,使用上一个示例中的标记:

Since Jul 12, 2016 the most_common() function actually has some additional arguments that you can specify. First of all you can specify a min_count which filters out the top tags that fall below a certain threshold. As an illustration using the tags from the previous example:

top_tags = User.tag.most_common()[:3]

返回前面指定但使用的所有三个标记

returns all three tags as specified earlier but using

top_tags = User.tag.most_common(min_count=2)[:3]

仅返回["fruits", "vegetables"],这是因为只有1个用户对象被标记为candy,这意味着它低于2的min_count

only returns ["fruits", "vegetables"] this is because only 1 User object was tagged with candy meaning that it falls below the min_count of 2

您可以为most_common提供的另一个参数是extra_filters,这使您能够提供一个对象,其中包含要用来过滤标记的其他过滤器值.

An additional argument that you can provide to most_common is extra_filters this enables you to provide an object containing additional filter values that you want to filter the tags by.

一个用法示例为:

filtered_users = User.objects.filter(age=20, is_delete=False)

    top_tags = User.tag.most_common(
        min_count=1, extra_filters={
            'user__in': filtered_users
        }
    )

在这里,我们创建一个过滤后的User对象查询集,然后将其提供给extra_filters参数,以将标签搜索限制在特定的子组中

Here we create a filtered queryset of User objects that we then provide to the extra_filters argument to limit the tag search to a specific subgroup

这篇关于如何过滤django-taggit顶部标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆