diff --git a/.gitignore b/.gitignore index 79981bbc..c685de76 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,6 @@ venv .tox/ .coverage .vscode/ +build/**/* +foo/**/* +foobar* diff --git a/binder/views.py b/binder/views.py index c12c1760..981c9c11 100644 --- a/binder/views.py +++ b/binder/views.py @@ -1406,7 +1406,6 @@ def store_m2m_field(obj, field, value, request): except BinderValidationError as e: validation_errors.append(e) - if validation_errors: raise sum(validation_errors, None) @@ -2002,6 +2001,10 @@ def _multi_put_save_objects(self, ordered_objects, objects, request): view._store(obj, values, request, pk=oid) except BinderValidationError as e: validation_errors.append(e) + except BinderFieldTypeError: + if not validation_errors: + raise + if oid < 0: new_id_map[(model, oid)] = obj.id for base in getmro(model)[1:]: diff --git a/tests/__init__.py b/tests/__init__.py index 4dcaf8bd..58ea2c8c 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -35,6 +35,7 @@ 'DEBUG': True, 'SECRET_KEY': 'testy mctestface', 'ALLOWED_HOSTS': ['*'], + 'SECRET_KEY': 'test_secret', 'DATABASES': { 'default': db_settings, }, diff --git a/tests/test_m2m_store_errors.py b/tests/test_m2m_store_errors.py index 0d5167da..732c8835 100644 --- a/tests/test_m2m_store_errors.py +++ b/tests/test_m2m_store_errors.py @@ -121,3 +121,55 @@ def test_saving_o2o_with_validation_error(self): response = self.client.put('/contact_person/', data=json.dumps(model_data), content_type='application/json') self.assert_validation_error_as_response(response) + + # Validation should be shown when contained in m2m relation + def test_saving_m2m_with_validation_error(self): + data = { + "id": -65, + # Name exceeds allowed 64 charecters, should show this error in response + 'name': 'Scooby Dooooooooooooooooooooooooooooooooooooooooooooooooooooooooo', + } + + zoo_data = { + "data": [ + { + "id": -4, + 'name': 2, + 'most_popular_animals': [], + "contacts": [], + 'opening_time': 'time validation error' + } + ] + } + + zoo_data_with_animal = { + "data": [ + { + "id": -4, + 'name': 2, + 'most_popular_animals': [-65], + "contacts": [], + 'opening_time': 'time validation error' + } + ], + "with": { + "animal": [data] + } + } + + animal_data = { + "data": [data] + } + + response_animal = self.client.put( + '/animal/', data=json.dumps(animal_data), content_type='application/json') + response_zoo = self.client.put( + '/zoo/', data=json.dumps(zoo_data), content_type='application/json') + response_zoo_with_animal = self.client.put( + '/zoo/', data=json.dumps(zoo_data_with_animal), content_type='application/json') + + self.assertEqual(response_zoo.status_code, 400) + self.assertIn(str(response_animal.content), + str(response_zoo_with_animal.content)) + self.assertIn(str(response_zoo.content), str( + response_zoo_with_animal.content))