How to filter django-taggit top tags

蹲街弑〆低调 提交于 2019-12-11 02:56:03

问题


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. How would you access and display information about the top X tags used within the Django app?

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


回答1:


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

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.

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

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()

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

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]

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


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

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]

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

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.

One usage example would be:

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
        }
    )

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




来源:https://stackoverflow.com/questions/51053595/how-to-filter-django-taggit-top-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!