2012-10-05 01:07:24 +04:00
|
|
|
from django.test import TestCase
|
|
|
|
from rest_framework import serializers
|
|
|
|
from rest_framework.tests.models import *
|
|
|
|
|
|
|
|
|
|
|
|
class TestGenericRelations(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
bookmark = Bookmark(url='https://www.djangoproject.com/')
|
|
|
|
bookmark.save()
|
|
|
|
django = Tag(tag_name='django')
|
|
|
|
django.save()
|
|
|
|
python = Tag(tag_name='python')
|
|
|
|
python.save()
|
|
|
|
t1 = TaggedItem(content_object=bookmark, tag=django)
|
|
|
|
t1.save()
|
|
|
|
t2 = TaggedItem(content_object=bookmark, tag=python)
|
|
|
|
t2.save()
|
|
|
|
self.bookmark = bookmark
|
|
|
|
|
|
|
|
def test_reverse_generic_relation(self):
|
|
|
|
class BookmarkSerializer(serializers.ModelSerializer):
|
2012-10-09 13:25:01 +04:00
|
|
|
tags = serializers.ManyRelatedField(source='tags')
|
2012-10-05 01:07:24 +04:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Bookmark
|
|
|
|
exclude = ('id',)
|
|
|
|
|
2012-11-05 14:53:20 +04:00
|
|
|
serializer = BookmarkSerializer(self.bookmark)
|
2012-10-05 01:07:24 +04:00
|
|
|
expected = {
|
|
|
|
'tags': [u'django', u'python'],
|
|
|
|
'url': u'https://www.djangoproject.com/'
|
|
|
|
}
|
|
|
|
self.assertEquals(serializer.data, expected)
|