aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/tests/genericrelations.py
diff options
context:
space:
mode:
authorTom Christie2013-01-25 14:36:27 +0000
committerTom Christie2013-01-25 14:36:27 +0000
commitb783887c33fa149112469af788d9c1f000bc4a2e (patch)
treefc394c6f11830f5d4c4d003fddda1dae1c0bb53c /rest_framework/tests/genericrelations.py
parentb73d7e9bb4158d5cbbd9121cda3131b6e0cafd79 (diff)
downloaddjango-rest-framework-b783887c33fa149112469af788d9c1f000bc4a2e.tar.bz2
Test for GFK, using RelatedField. Refs #607.
Diffstat (limited to 'rest_framework/tests/genericrelations.py')
-rw-r--r--rest_framework/tests/genericrelations.py57
1 files changed, 52 insertions, 5 deletions
diff --git a/rest_framework/tests/genericrelations.py b/rest_framework/tests/genericrelations.py
index dcdd8329..146ad1e4 100644
--- a/rest_framework/tests/genericrelations.py
+++ b/rest_framework/tests/genericrelations.py
@@ -12,7 +12,7 @@ class Tag(models.Model):
tag = models.SlugField()
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
- content_object = GenericForeignKey('content_type', 'object_id')
+ tagged_item = GenericForeignKey('content_type', 'object_id')
def __unicode__(self):
return self.tag
@@ -25,20 +25,37 @@ class Bookmark(models.Model):
url = models.URLField()
tags = GenericRelation(Tag)
+ def __unicode__(self):
+ return 'Bookmark: %s' % self.url
+
+
+class Note(models.Model):
+ """
+ A textual note that may have multiple tags attached.
+ """
+ text = models.TextField()
+ tags = GenericRelation(Tag)
+
+ def __unicode__(self):
+ return 'Note: %s' % self.text
+
class TestGenericRelations(TestCase):
def setUp(self):
self.bookmark = Bookmark.objects.create(url='https://www.djangoproject.com/')
- Tag.objects.create(content_object=self.bookmark, tag='django')
- Tag.objects.create(content_object=self.bookmark, tag='python')
+ Tag.objects.create(tagged_item=self.bookmark, tag='django')
+ Tag.objects.create(tagged_item=self.bookmark, tag='python')
+ self.note = Note.objects.create(text='Remember the milk')
+ Tag.objects.create(tagged_item=self.note, tag='reminder')
- def test_reverse_generic_relation(self):
+ def test_generic_relation(self):
"""
Test a relationship that spans a GenericRelation field.
+ IE. A reverse generic relationship.
"""
class BookmarkSerializer(serializers.ModelSerializer):
- tags = serializers.ManyRelatedField(source='tags')
+ tags = serializers.ManyRelatedField()
class Meta:
model = Bookmark
@@ -50,3 +67,33 @@ class TestGenericRelations(TestCase):
'url': u'https://www.djangoproject.com/'
}
self.assertEquals(serializer.data, expected)
+
+ def test_generic_fk(self):
+ """
+ Test a relationship that spans a GenericForeignKey field.
+ IE. A forward generic relationship.
+ """
+
+ class TagSerializer(serializers.ModelSerializer):
+ tagged_item = serializers.RelatedField()
+
+ class Meta:
+ model = Tag
+ exclude = ('id', 'content_type', 'object_id')
+
+ serializer = TagSerializer(Tag.objects.all())
+ expected = [
+ {
+ 'tag': u'django',
+ 'tagged_item': u'Bookmark: https://www.djangoproject.com/'
+ },
+ {
+ 'tag': u'python',
+ 'tagged_item': u'Bookmark: https://www.djangoproject.com/'
+ },
+ {
+ 'tag': u'reminder',
+ 'tagged_item': u'Note: Remember the milk'
+ }
+ ]
+ self.assertEquals(serializer.data, expected)