mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-30 13:34:00 +03:00
36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
from __future__ import unicode_literals
|
|
|
|
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):
|
|
tags = serializers.ManyRelatedField(source='tags')
|
|
|
|
class Meta:
|
|
model = Bookmark
|
|
exclude = ('id',)
|
|
|
|
serializer = BookmarkSerializer(self.bookmark)
|
|
expected = {
|
|
'tags': ['django', 'python'],
|
|
'url': 'https://www.djangoproject.com/'
|
|
}
|
|
self.assertEquals(serializer.data, expected)
|