mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-09 08:00:52 +03:00
Allow 'None' to pass as a null value in RelatedFields
This commit is contained in:
parent
505f1173d0
commit
f126856f65
|
@ -33,6 +33,7 @@ class RelatedField(WritableField):
|
||||||
many_widget = widgets.SelectMultiple
|
many_widget = widgets.SelectMultiple
|
||||||
form_field_class = forms.ChoiceField
|
form_field_class = forms.ChoiceField
|
||||||
many_form_field_class = forms.MultipleChoiceField
|
many_form_field_class = forms.MultipleChoiceField
|
||||||
|
null_values = (None, '', 'None')
|
||||||
|
|
||||||
cache_choices = False
|
cache_choices = False
|
||||||
empty_label = None
|
empty_label = None
|
||||||
|
@ -168,9 +169,9 @@ class RelatedField(WritableField):
|
||||||
return
|
return
|
||||||
value = [] if self.many else None
|
value = [] if self.many else None
|
||||||
|
|
||||||
if value in (None, '') and self.required:
|
if value in self.null_values:
|
||||||
|
if self.required:
|
||||||
raise ValidationError(self.error_messages['required'])
|
raise ValidationError(self.error_messages['required'])
|
||||||
elif value in (None, ''):
|
|
||||||
into[(self.source or field_name)] = None
|
into[(self.source or field_name)] = None
|
||||||
elif self.many:
|
elif self.many:
|
||||||
into[(self.source or field_name)] = [self.from_native(item) for item in value]
|
into[(self.source or field_name)] = [self.from_native(item) for item in value]
|
||||||
|
|
|
@ -15,12 +15,12 @@ urlpatterns = patterns(
|
||||||
|
|
||||||
class NullableForeignKeyTests(APITestCase):
|
class NullableForeignKeyTests(APITestCase):
|
||||||
"""
|
"""
|
||||||
DRF should be able to handle nullable fields when a TestClient
|
DRF should be able to handle nullable foreign keys when a test
|
||||||
POST/PUT request is made with its own serialized object.
|
Client POST/PUT request is made with its own serialized object.
|
||||||
"""
|
"""
|
||||||
urls = 'rest_framework.tests.test_nullable_fields'
|
urls = 'rest_framework.tests.test_nullable_fields'
|
||||||
|
|
||||||
def test_updating_object_with_null_field_value(self):
|
def test_updating_object_with_null_fk(self):
|
||||||
obj = NullableForeignKeySource(name='example', target=None)
|
obj = NullableForeignKeySource(name='example', target=None)
|
||||||
obj.save()
|
obj.save()
|
||||||
serialized_data = NullableFKSourceSerializer(obj).data
|
serialized_data = NullableFKSourceSerializer(obj).data
|
||||||
|
|
Loading…
Reference in New Issue
Block a user