Commit f126856f by Dustin Farris

Allow 'None' to pass as a null value in RelatedFields

parent 505f1173
......@@ -33,6 +33,7 @@ class RelatedField(WritableField):
many_widget = widgets.SelectMultiple
form_field_class = forms.ChoiceField
many_form_field_class = forms.MultipleChoiceField
null_values = (None, '', 'None')
cache_choices = False
empty_label = None
......@@ -168,9 +169,9 @@ class RelatedField(WritableField):
return
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'])
elif value in (None, ''):
into[(self.source or field_name)] = None
elif self.many:
into[(self.source or field_name)] = [self.from_native(item) for item in value]
......
......@@ -15,12 +15,12 @@ urlpatterns = patterns(
class NullableForeignKeyTests(APITestCase):
"""
DRF should be able to handle nullable fields when a TestClient
POST/PUT request is made with its own serialized object.
DRF should be able to handle nullable foreign keys when a test
Client POST/PUT request is made with its own serialized object.
"""
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.save()
serialized_data = NullableFKSourceSerializer(obj).data
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment