3

I'm trying to get an ordered list of the most-used tags on my site. The API docs for django taggit reference a most_common() method, but I can't seem to make it work. Docs say:

"Returns a QuerySet of all tags, annotated with the number of times they appear, available as the num_times attribute on each tag."

But they don't show a working example. Assuming that an "Item" model has a working "tags" field, does this mean you should be able to do something like:

Item.objects.all.tags.most_common()

I've tried variations of this but can't seem to get an ordered list of most-used tags. What's the magic incantation expected here?

shacker
  • 14,712
  • 8
  • 89
  • 89

1 Answers1

6

I believe it should be without the objects.all:

tags = Item.tags.most_common()
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
  • 1
    Also, I'm curious how you figured that out from the API docs, if you don't mind a quick explainer. – shacker Feb 14 '12 at 16:11
  • 1
    `tags` is a TaggableManager object, which is a subclass of a [Manager](https://docs.djangoproject.com/en/dev/topics/db/managers/). It works the same way as `objects`, so typical usage is `ModelClass.manager_instance.query_modification_method()` – Mariusz Jamro Feb 14 '12 at 16:18