Skip to content

Commit

Permalink
Change MultipleModelField to error on non-iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
c17r authored and mixxorz committed Mar 21, 2018
1 parent 0f594a9 commit 24dbbfe
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
4 changes: 3 additions & 1 deletion service_objects/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ class MultipleModelField(ModelField):
the database
"""
error_non_list = _("Object is not iterable")

def clean(self, values):
if not isinstance(values, list):
values = [values]
raise ValidationError(self.error_non_list)

for value in values:
self.check_type(value)
Expand Down
7 changes: 2 additions & 5 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,13 @@ def test_multiple_unsaved_false(self):
with self.assertRaisesRegexp(ValidationError, "[Uu]nsaved"):
cleaned_data = model_field.clean(objects)


def test_multiple_non_list(self):
model_field = MultipleModelField(FooModel)
obj = FooModel(one='a')
obj.pk = 1

cleaned_data = model_field.clean(obj)

self.assertTrue(isinstance(cleaned_data, list))
self.assertEqual(obj, cleaned_data[0])
with self.assertRaisesRegexp(ValidationError, "[Ii]terable"):
cleaned_data = model_field.clean(obj)

def test_multiple_valid(self):
model_field = MultipleModelField(FooModel)
Expand Down

0 comments on commit 24dbbfe

Please sign in to comment.