diff options
| author | Dustin Farris | 2014-02-27 12:17:32 -0500 |
|---|---|---|
| committer | Dustin Farris | 2014-02-27 12:30:59 -0500 |
| commit | f126856f65aa86de2c4cc1b1e3bb6a52cebb34b8 (patch) | |
| tree | 6070a44ea4095026d162684f204766a6df9e1b3e /rest_framework | |
| parent | 505f1173d0c5a20ea804cad3b503c4bf55cd04d8 (diff) | |
| download | django-rest-framework-f126856f65aa86de2c4cc1b1e3bb6a52cebb34b8.tar.bz2 | |
Allow 'None' to pass as a null value in RelatedFields
Diffstat (limited to 'rest_framework')
| -rw-r--r-- | rest_framework/relations.py | 7 | ||||
| -rw-r--r-- | rest_framework/tests/test_nullable_fields.py | 6 |
2 files changed, 7 insertions, 6 deletions
diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 02185c2f..163a8984 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -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: - raise ValidationError(self.error_messages['required']) - elif value in (None, ''): + if value in self.null_values: + if self.required: + raise ValidationError(self.error_messages['required']) into[(self.source or field_name)] = None elif self.many: into[(self.source or field_name)] = [self.from_native(item) for item in value] diff --git a/rest_framework/tests/test_nullable_fields.py b/rest_framework/tests/test_nullable_fields.py index 556543d9..6ee55c00 100644 --- a/rest_framework/tests/test_nullable_fields.py +++ b/rest_framework/tests/test_nullable_fields.py @@ -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 |
