Skip to content

Commit 1687108

Browse files
committed
Merge pull request #1844 from adamsc64/issue_1533
Fixed #1533 - Resolved issue with integer keys on nested choices never v...
2 parents dc27953 + 613a301 commit 1687108

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

rest_framework/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ def valid_value(self, value):
563563
if isinstance(v, (list, tuple)):
564564
# This is an optgroup, so look inside the group for options
565565
for k2, v2 in v:
566-
if value == smart_text(k2):
566+
if value == smart_text(k2) or value == k2:
567567
return True
568568
else:
569569
if value == smart_text(k) or value == k:

tests/test_validation.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import unicode_literals
22
from django.core.validators import MaxValueValidator
3+
from django.core.exceptions import ValidationError
34
from django.db import models
45
from django.test import TestCase
56
from rest_framework import generics, serializers, status
@@ -146,3 +147,42 @@ def test_max_value_validation_fail(self):
146147
response = view(request, pk=obj.pk).render()
147148
self.assertEqual(response.content, b'{"number_value": ["Ensure this value is less than or equal to 100."]}')
148149
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
150+
151+
152+
class TestChoiceFieldChoicesValidate(TestCase):
153+
CHOICES = [
154+
(0, 'Small'),
155+
(1, 'Medium'),
156+
(2, 'Large'),
157+
]
158+
159+
CHOICES_NESTED = [
160+
('Category', (
161+
(1, 'First'),
162+
(2, 'Second'),
163+
(3, 'Third'),
164+
)),
165+
(4, 'Fourth'),
166+
]
167+
168+
def test_choices(self):
169+
"""
170+
Make sure a value for choices works as expected.
171+
"""
172+
f = serializers.ChoiceField(choices=self.CHOICES)
173+
value = self.CHOICES[0][0]
174+
try:
175+
f.validate(value)
176+
except ValidationError:
177+
self.fail("Value %s does not validate" % str(value))
178+
179+
def test_nested_choices(self):
180+
"""
181+
Make sure a nested value for choices works as expected.
182+
"""
183+
f = serializers.ChoiceField(choices=self.CHOICES_NESTED)
184+
value = self.CHOICES_NESTED[0][1][0][0]
185+
try:
186+
f.validate(value)
187+
except ValidationError:
188+
self.fail("Value %s does not validate" % str(value))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy