mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-09 08:00:52 +03:00
Adds test that blank option is added when required=False on RelatedFields
This commit is contained in:
parent
60ac3d7a76
commit
c1ac65edce
|
@ -59,6 +59,8 @@ class RelatedField(WritableField):
|
||||||
super(RelatedField, self).__init__(*args, **kwargs)
|
super(RelatedField, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
if not self.required:
|
if not self.required:
|
||||||
|
# Accessed in ModelChoiceIterator django/forms/models.py:1034
|
||||||
|
# If set adds empty choice.
|
||||||
self.empty_label = BLANK_CHOICE_DASH[0][1]
|
self.empty_label = BLANK_CHOICE_DASH[0][1]
|
||||||
|
|
||||||
self.queryset = queryset
|
self.queryset = queryset
|
||||||
|
|
|
@ -118,3 +118,25 @@ class RelatedFieldSourceTests(TestCase):
|
||||||
(serializers.ModelSerializer,), attrs)
|
(serializers.ModelSerializer,), attrs)
|
||||||
with self.assertRaises(AttributeError):
|
with self.assertRaises(AttributeError):
|
||||||
TestSerializer(data={'name': 'foo'})
|
TestSerializer(data={'name': 'foo'})
|
||||||
|
|
||||||
|
|
||||||
|
class RelatedFieldChoicesTests(TestCase):
|
||||||
|
"""
|
||||||
|
Tests for #1408 "Web browseable API doesn't have blank option on drop down list box"
|
||||||
|
https://github.com/tomchristie/django-rest-framework/issues/1408
|
||||||
|
"""
|
||||||
|
def test_blank_option_is_added_to_choice_if_required_equals_false(self):
|
||||||
|
"""
|
||||||
|
|
||||||
|
"""
|
||||||
|
post = BlogPost(title="Checking blank option is added")
|
||||||
|
post.save()
|
||||||
|
|
||||||
|
queryset = BlogPost.objects.all()
|
||||||
|
field = serializers.RelatedField(required=False, queryset=queryset)
|
||||||
|
|
||||||
|
choice_count = BlogPost.objects.count()
|
||||||
|
widget_count = len(field.widget.choices)
|
||||||
|
|
||||||
|
self.assertEqual(widget_count, choice_count + 1, 'BLANK_CHOICE_DASH option should have been added')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user