Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Reworked ErrorDict.as_json() to prevent unnecessary serialization/des…

…erialization step.

Thanks @apollo13 for the suggestion. Refs #17413.
  • Loading branch information...
commit 34236efc5e569ce7d42f7d52dd798e59f95457f8 1 parent 9b729dd
@loic loic authored apollo13 committed
Showing with 13 additions and 11 deletions.
  1. +13 −11 django/forms/utils.py
View
24 django/forms/utils.py
@@ -4,6 +4,11 @@
import sys
import warnings
+try:
+ from collections import UserList
+except ImportError: # Python 2
+ from UserList import UserList
+
from django.conf import settings
from django.utils.html import format_html, format_html_join, escape
from django.utils.encoding import force_text, python_2_unicode_compatible
@@ -15,11 +20,6 @@
# module to maintain backwards compatibility.
from django.core.exceptions import ValidationError
-try:
- from collections import UserList
-except ImportError: # Python 2
- from UserList import UserList
-
def flatatt(attrs):
"""
@@ -56,8 +56,7 @@ def as_data(self):
return {f: e.as_data() for f, e in self.items()}
def as_json(self, escape_html=False):
- errors = {f: json.loads(e.as_json(escape_html=escape_html)) for f, e in self.items()}
- return json.dumps(errors)
+ return json.dumps({f: e.get_json_data(escape_html) for f, e in self.items()})
def as_ul(self):
if not self:
@@ -84,17 +83,20 @@ class ErrorList(UserList, list):
A collection of errors that knows how to display itself in various formats.
"""
def as_data(self):
- return self.data
+ return ValidationError(self.data).error_list
- def as_json(self, escape_html=False):
+ def get_json_data(self, escape_html=False):
errors = []
- for error in ValidationError(self.data).error_list:
+ for error in self.as_data():
message = list(error)[0]
errors.append({
'message': escape(message) if escape_html else message,
'code': error.code or '',
})
- return json.dumps(errors)
+ return errors
+
+ def as_json(self, escape_html=False):
+ return json.dumps(self.get_json_data(escape_html))
def as_ul(self):
if not self.data:
Please sign in to comment.
Something went wrong with that request. Please try again.