def top_tags(self, min_count=2, limit=20):
        if not taggit:
            return []

        # TODO: There has got to be a better way to do this. I got lost in
        # Django model bits, though.

        # Gather list of tags sorted by use frequency
        ct = ContentType.objects.get_for_model(Badge)
        tag_counts = (TaggedItem.objects
            .values('tag')
            .annotate(count=Count('id'))
            .filter(content_type=ct, count__gte=min_count)
            .order_by('-count'))[:limit]

        # Gather set of tag IDs from list
        tag_ids = set(x['tag'] for x in tag_counts)

        # Gather and map tag objects to IDs
        tags_by_id = dict((x.pk, x)
            for x in Tag.objects.filter(pk__in=tag_ids))

        # Join tag objects up with counts
        tags_with_counts = [
            dict(count=x['count'], tag=tags_by_id[x['tag']])
            for x in tag_counts]

        return tags_with_counts
Assemble list of top-used tags
dataset/ETHPy150Open mozilla/django-badger/badger/models.py/BadgeManager.top_tags