From 7a878ca5cb50ad65fc465cb263a44cc93629f75c Mon Sep 17 00:00:00 2001 From: Andrey Maslov Date: Tue, 23 Dec 2014 17:51:36 +0200 Subject: [PATCH] Fixed #24008 -- Fixed ValidationError crash with list of dicts. --- django/core/exceptions.py | 5 ++++- docs/releases/1.7.2.txt | 3 +++ tests/forms_tests/tests/test_utils.py | 26 ++++++++++++++++++++++++-- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/django/core/exceptions.py b/django/core/exceptions.py index a8b432b1c6..a33fb36ea5 100644 --- a/django/core/exceptions.py +++ b/django/core/exceptions.py @@ -115,7 +115,10 @@ class ValidationError(Exception): # Normalize plain strings to instances of ValidationError. if not isinstance(message, ValidationError): message = ValidationError(message) - self.error_list.extend(message.error_list) + if hasattr(message, 'error_dict'): + self.error_list.extend(sum(message.error_dict.values(), [])) + else: + self.error_list.extend(message.error_list) else: self.message = message diff --git a/docs/releases/1.7.2.txt b/docs/releases/1.7.2.txt index c4938530e4..1070946d83 100644 --- a/docs/releases/1.7.2.txt +++ b/docs/releases/1.7.2.txt @@ -186,3 +186,6 @@ Bugfixes * Restored the ability to use more than five levels of subqueries (:ticket:`23758`). + +* Fixed crash when ``ValidationError`` is initialized with a ``ValidationError`` + that is initialized with a dictionary (:ticket:`24008`). diff --git a/tests/forms_tests/tests/test_utils.py b/tests/forms_tests/tests/test_utils.py index 6e40e7565c..9f412b7a27 100644 --- a/tests/forms_tests/tests/test_utils.py +++ b/tests/forms_tests/tests/test_utils.py @@ -65,9 +65,31 @@ class FormsUtilsTestCase(TestCase): self.assertHTMLEqual(str(ErrorList(ValidationError(["Error one.", "Error two."]).messages)), '') + # Can take a dict. + self.assertHTMLEqual( + str(ErrorList(sorted(ValidationError({'error_1': "1. Error one.", 'error_2': "2. Error two."}).messages))), + '' + ) + # Can take a mixture in a list. - self.assertHTMLEqual(str(ErrorList(ValidationError(["First error.", "Not \u03C0.", ugettext_lazy("Error.")]).messages)), - '') + self.assertHTMLEqual( + str(ErrorList(sorted(ValidationError([ + "1. First error.", + "2. Not \u03C0.", + ugettext_lazy("3. Error."), + { + 'error_1': "4. First dict error.", + 'error_2': "5. Second dict error.", + }, + ]).messages))), + '' + ) @python_2_unicode_compatible class VeryBadError: