Commit b4c21524 by Alex Gaynor

No longer allow trying to get tags for an unsaved instance. Running the test…

No longer allow trying to get tags for an unsaved instance.  Running the test suite now requires python 2.5 (I'll roll this back if it affects anyone unduely).
parent f7fad676
...@@ -39,6 +39,9 @@ class TaggableManager(object): ...@@ -39,6 +39,9 @@ class TaggableManager(object):
manager.model = type manager.model = type
if instance is None: if instance is None:
manager.object_id = None manager.object_id = None
elif instance.pk is None:
raise ValueError("%s objects need to have a primary key value "
"before you can access their tags." % type.__name__)
else: else:
manager.object_id = instance.pk manager.object_id = instance.pk
return manager return manager
...@@ -86,7 +89,9 @@ class TaggableManager(object): ...@@ -86,7 +89,9 @@ class TaggableManager(object):
return form_class(**kwargs) return form_class(**kwargs)
def value_from_object(self, instance): def value_from_object(self, instance):
return ", ".join(map(unicode, getattr(instance, self.name).all())) if instance.pk:
return ", ".join(map(unicode, getattr(instance, self.name).all()))
return ""
def related_query_name(self): def related_query_name(self):
return None return None
......
from __future__ import with_statement
from contextlib import contextmanager
from django.test import TestCase from django.test import TestCase
from taggit.models import Tag from taggit.models import Tag
...@@ -12,7 +15,17 @@ class BaseTaggingTest(TestCase): ...@@ -12,7 +15,17 @@ class BaseTaggingTest(TestCase):
got.sort() got.sort()
tags.sort() tags.sort()
self.assertEqual(got, tags) self.assertEqual(got, tags)
@contextmanager
def assert_raises(self, exc_type):
try:
yield
except Exception, e:
self.assert_(type(e) is exc_type, "%s didn't match expected "
"exception type %s" % (e, exc_type))
else:
self.fail("No exception raised, expected %s" % exc_type)
class AddTagTestCase(BaseTaggingTest): class AddTagTestCase(BaseTaggingTest):
def test_add_tag(self): def test_add_tag(self):
...@@ -44,6 +57,10 @@ class AddTagTestCase(BaseTaggingTest): ...@@ -44,6 +57,10 @@ class AddTagTestCase(BaseTaggingTest):
apple.delete() apple.delete()
self.assert_tags_equal(Food.tags.all(), ["green"]) self.assert_tags_equal(Food.tags.all(), ["green"])
f = Food()
with self.assert_raises(ValueError):
f.tags.all()
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