Commit 46a4e80a by Alex Gaynor

Added a most common method to tags managers

parent 76c8a84f
......@@ -51,3 +51,8 @@ class _TaggableManager(models.Manager):
def clear(self):
TaggedItem.objects.filter(object_id=self.object_id,
content_type=ContentType.objects.get_for_model(self.model)).delete()
def most_common(self):
return self.get_query_set().annotate(
num_times=models.Count('items')
).order_by('-num_times')
......@@ -27,6 +27,8 @@ class AddTagTestCase(BaseTaggingTest):
self.assert_tags_equal(apple.tags.all(), ['green', 'red'])
self.assert_tags_equal(Food.tags.all(), ['green', 'red'])
self.assert_tags_equal(Food.tags.most_common(), ['green', 'red'])
apple.tags.remove('green')
self.assert_tags_equal(apple.tags.all(), ['red'])
self.assert_tags_equal(Food.tags.all(), ['green', 'red'])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment