From 738e9e615dc81b561c9fb01439119f4475c2e25b Mon Sep 17 00:00:00 2001 From: Baptiste Mispelon Date: Wed, 11 Dec 2019 19:36:57 +0100 Subject: [PATCH] Fixed #26743 -- Fixed UnboundLocalError crash when deserializing m2m fields and value isn't iterable. --- django/core/serializers/base.py | 6 +++++- tests/serializers/test_json.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/django/core/serializers/base.py b/django/core/serializers/base.py index cf8e7d3b862..72565236c4e 100644 --- a/django/core/serializers/base.py +++ b/django/core/serializers/base.py @@ -282,9 +282,13 @@ def deserialize_m2m_values(field, field_value, using, handle_forward_references) def m2m_convert(v): return model._meta.pk.to_python(v) + try: + pks_iter = iter(field_value) + except TypeError as e: + raise M2MDeserializationError(e, field_value) try: values = [] - for pk in field_value: + for pk in pks_iter: values.append(m2m_convert(pk)) return values except Exception as e: diff --git a/tests/serializers/test_json.py b/tests/serializers/test_json.py index a804aa1f3a5..93e51e124c6 100644 --- a/tests/serializers/test_json.py +++ b/tests/serializers/test_json.py @@ -252,6 +252,20 @@ class JsonSerializerTestCase(SerializersTestBase, TestCase): for obj in serializers.deserialize('json', test_string, ignore=False): obj.save() + def test_helpful_error_message_for_many2many_not_iterable(self): + """ + Not iterable many-to-many field value throws a helpful error message. + """ + test_string = """[{ + "pk": 1, + "model": "serializers.m2mdata", + "fields": {"data": null} + }]""" + + expected = "(serializers.m2mdata:pk=1) field_value was 'None'" + with self.assertRaisesMessage(DeserializationError, expected): + next(serializers.deserialize('json', test_string, ignore=False)) + class JsonSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase): serializer_name = "json"