Commit fdcce8d0 by Alex Gaynor

You can add tag objects.

parent 2172cc0b
...@@ -105,6 +105,7 @@ class _TaggableManager(models.Manager): ...@@ -105,6 +105,7 @@ class _TaggableManager(models.Manager):
@require_instance_manager @require_instance_manager
def add(self, *tags): def add(self, *tags):
for tag in tags: for tag in tags:
if not isinstance(tag, Tag):
tag, _ = Tag.objects.get_or_create(name=tag) tag, _ = Tag.objects.get_or_create(name=tag)
TaggedItem.objects.create(object_id=self.object_id, TaggedItem.objects.create(object_id=self.object_id,
content_type=ContentType.objects.get_for_model(self.model), tag=tag) content_type=ContentType.objects.get_for_model(self.model), tag=tag)
......
...@@ -34,6 +34,9 @@ class AddTagTestCase(BaseTaggingTest): ...@@ -34,6 +34,9 @@ class AddTagTestCase(BaseTaggingTest):
apple.tags.remove('green') apple.tags.remove('green')
self.assert_tags_equal(apple.tags.all(), ['red']) self.assert_tags_equal(apple.tags.all(), ['red'])
self.assert_tags_equal(Food.tags.all(), ['green', 'red']) self.assert_tags_equal(Food.tags.all(), ['green', 'red'])
tag = Tag.objects.create(name="delicious")
apple.tags.add(tag)
self.assert_tags_equal(apple.tags.all(), ["red", "delicious"])
class LookupByTagTestCase(BaseTaggingTest): class LookupByTagTestCase(BaseTaggingTest):
......
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