Merge pull request #632 from fernandogrd/master

Fix processing of ManyToManyField when it is empty
This commit is contained in:
Tom Christie 2013-01-30 04:40:39 -08:00
commit 2948305b98
2 changed files with 15 additions and 1 deletions

View File

@ -148,7 +148,7 @@ class ManyRelatedMixin(object):
value = data.getlist(self.source or field_name)
except:
# Non-form data
value = data.get(self.source or field_name)
value = data.get(self.source or field_name, [])
else:
if value == ['']:
value = []

View File

@ -31,3 +31,17 @@ class FieldTests(TestCase):
field = serializers.SlugRelatedField(queryset=NullModel.objects.all(), slug_field='pk')
self.assertRaises(serializers.ValidationError, field.from_native, '')
self.assertRaises(serializers.ValidationError, field.from_native, [])
class TestManyRelateMixin(TestCase):
def test_missing_many_to_many_related_field(self):
'''
Regression test for #632
https://github.com/tomchristie/django-rest-framework/pull/632
'''
field = serializers.ManyRelatedField(read_only=False)
into = {}
field.field_from_native({}, None, 'field_name', into)
self.assertEqual(into['field_name'], [])