Commit 62d88910 by Ben Cardy

Added names() method to _TaggableManager

parent 57055742
...@@ -54,6 +54,16 @@ playing around with the API. ...@@ -54,6 +54,16 @@ playing around with the API.
model with its own tagging through table, only other instances of the model with its own tagging through table, only other instances of the
same model will be returned. same model will be returned.
.. method:: names()
Convenience method, returning a ``ValuesListQuerySet`` (basically
just an iterable) containing the name of each tag as a string::
>>> apple.tags.names()
[u'green', u'red']
>>> 'green' in apple.tags.names()
True
Filtering Filtering
~~~~~~~~~ ~~~~~~~~~
......
...@@ -172,6 +172,10 @@ class _TaggableManager(models.Manager): ...@@ -172,6 +172,10 @@ class _TaggableManager(models.Manager):
self.through.objects.get_or_create(tag=tag, **self._lookup_kwargs()) self.through.objects.get_or_create(tag=tag, **self._lookup_kwargs())
@require_instance_manager @require_instance_manager
def names(self):
return self.get_query_set().values_list('name', flat=True)
@require_instance_manager
def set(self, *tags): def set(self, *tags):
self.clear() self.clear()
self.add(*tags) self.add(*tags)
......
...@@ -313,6 +313,12 @@ class TaggableManagerTestCase(BaseTaggingTestCase): ...@@ -313,6 +313,12 @@ class TaggableManagerTestCase(BaseTaggingTestCase):
self.assertTrue(hasattr(field, 'related')) self.assertTrue(hasattr(field, 'related'))
self.assertEqual(self.food_model, field.related.model) self.assertEqual(self.food_model, field.related.model)
def test_names_method(self):
apple = self.food_model.objects.create(name="apple")
apple.tags.add('green')
apple.tags.add('red')
self.assertEqual(list(apple.tags.names()), ['green', 'red'])
class TaggableManagerDirectTestCase(TaggableManagerTestCase): class TaggableManagerDirectTestCase(TaggableManagerTestCase):
food_model = DirectFood food_model = DirectFood
......
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