Enhancement in most_common
Django tagging provides usage_for_queryset, where we can pass a queryset and it returns a list of tags associated with instances of a model contained in the given queryset. Also it annotates each Tag. https://github.com/brosner/django-tagging/blob/master/tagging/models.py#L151
Taggit provides most_common() which is very much similar to it. But taggit doesn't provide a way so that user can specify a queryset.
eg:
class Entry:
is_published = models.BooleanField()
tags = TaggableManager()
If I say,
Entry.tags.most_common()
it will give me the list of tags used on any instance of Entry. It will include drafts as well as published Entries.
What if I only want to get the list of tags used on Published entries, i.e the entries which have is_published=True.
Try changing models.py to add this:
def most_common(self, **filters):
return self.get_query_set().filter(**filters).annotate(
num_times=models.Count(self.through.tag_relname())
).order_by('-num_times')
Then you can query like this:
filters = {'is_published': True}
tags = Entry.tags.most_common(**filters)
I'll try to roll this into a pull request if I can work out how to write a test.
We have a way to pass in extra filters to most_common now, but I do basically agree that it would be cool to pass in an external queryset in general. Going to leave this one open for now