简体   繁体   中英

Top 7 most common tags Taggit

def most_common(self):
    return self.get_queryset().annotate(
        num_times=models.Count(self.through.tag_relname())
        ).order_by('-num_times')

How would I edit this to only view the top 7 tags. This is a function from taggit and I would only like to see only 7. thanks.

How about most_common()[:7]?

YourModel.tags.most_common()[:7]

docs.djangoproject.com/en/1.10/topics/db/queries/… – ozgur

This is the correct asnwer, thanks ozgur

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM