This commit is contained in:
José Padilla 2017-03-27 19:16:20 +00:00 committed by GitHub
commit 3ae276ba57
2 changed files with 35 additions and 3 deletions

View File

@ -123,10 +123,7 @@ def get_field_kwargs(field_name, model_field):
kwargs['allow_folders'] = model_field.allow_folders kwargs['allow_folders'] = model_field.allow_folders
if model_field.choices: if model_field.choices:
# If this model field contains choices, then return early.
# Further keyword arguments are not valid.
kwargs['choices'] = model_field.choices kwargs['choices'] = model_field.choices
return kwargs
# Our decimal validation is handled in the field code, not validator code. # Our decimal validation is handled in the field code, not validator code.
# (In Django 1.9+ this differs from previous style) # (In Django 1.9+ this differs from previous style)

View File

@ -9,6 +9,7 @@ from collections import Mapping
import pytest import pytest
from django.db import models from django.db import models
from django.test import TestCase
from rest_framework import fields, relations, serializers from rest_framework import fields, relations, serializers
from rest_framework.compat import unicode_repr from rest_framework.compat import unicode_repr
@ -519,3 +520,37 @@ class TestDeclaredFieldInheritance:
assert len(Parent().get_fields()) == 2 assert len(Parent().get_fields()) == 2
assert len(Child().get_fields()) == 2 assert len(Child().get_fields()) == 2
assert len(Grandchild().get_fields()) == 2 assert len(Grandchild().get_fields()) == 2
CHOICES = (
('choice1', 'choice 1'),
('choice2', 'choice 1'),
)
class Poll(models.Model):
form_name = models.CharField(
'name', max_length=254, unique=True, choices=CHOICES
)
class Test5004(TestCase):
def test_unique_choice_field(self):
Poll.objects.create(form_name='choice1')
class PollSerializer(serializers.ModelSerializer):
class Meta:
model = Poll
fields = '__all__'
serializer = PollSerializer(data={'form_name': 'choice1'})
with self.assertRaises(serializers.ValidationError) as excinfo:
serializer.is_valid(raise_exception=True)
assert excinfo.exception.get_full_details() == {
'form_name': [{
'message': 'poll with this name already exists.',
'code': 'unique'
}]
}