mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 20:40:14 +03:00
Adds test for RelatedField choices
- Test tries to derive from RelatedField and return a dict in the to_representation method. This current raises a TypeError.
This commit is contained in:
parent
238783f2ed
commit
75c80087b8
|
@ -9,6 +9,7 @@ from __future__ import unicode_literals
|
||||||
|
|
||||||
import decimal
|
import decimal
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
import pytest
|
||||||
|
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.core.validators import (
|
from django.core.validators import (
|
||||||
|
@ -642,6 +643,22 @@ class TestRelationalFieldDisplayValue(TestCase):
|
||||||
expected = OrderedDict([(1, 'My Red Color'), (2, 'My Yellow Color'), (3, 'My Green Color')])
|
expected = OrderedDict([(1, 'My Red Color'), (2, 'My Yellow Color'), (3, 'My Green Color')])
|
||||||
self.assertEqual(serializer.fields['color'].choices, expected)
|
self.assertEqual(serializer.fields['color'].choices, expected)
|
||||||
|
|
||||||
|
def test_to_representation_returns_dict_type(self):
|
||||||
|
class TestField(serializers.RelatedField):
|
||||||
|
def to_representation(self, value):
|
||||||
|
return {'id': value.id}
|
||||||
|
|
||||||
|
class TestSerializer(serializers.ModelSerializer):
|
||||||
|
color = TestField(queryset=DisplayValueTargetModel.objects.all())
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = DisplayValueModel
|
||||||
|
fields = '__all__'
|
||||||
|
|
||||||
|
serializer = TestSerializer()
|
||||||
|
expected = OrderedDict([("{'id': 1}", 'Red Color'), ("{'id': 2}", 'Yellow Color'), ("{'id': 3}", 'Green Color')])
|
||||||
|
self.assertEqual(serializer.fields['color'].choices, expected)
|
||||||
|
|
||||||
|
|
||||||
class TestIntegration(TestCase):
|
class TestIntegration(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user