Used @cached_property for BaseForm.changed_data.

This commit is contained in:
Curtis Maloney 2015-08-04 10:14:15 +10:00 committed by Tim Graham
parent 9f73009e98
commit c6c00fbfbb
1 changed files with 23 additions and 24 deletions

View File

@ -16,6 +16,7 @@ from django.utils import six
from django.utils.encoding import ( from django.utils.encoding import (
force_text, python_2_unicode_compatible, smart_text, force_text, python_2_unicode_compatible, smart_text,
) )
from django.utils.functional import cached_property
from django.utils.html import conditional_escape, format_html, html_safe from django.utils.html import conditional_escape, format_html, html_safe
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
@ -92,7 +93,6 @@ class BaseForm(object):
self.label_suffix = label_suffix if label_suffix is not None else _(':') self.label_suffix = label_suffix if label_suffix is not None else _(':')
self.empty_permitted = empty_permitted self.empty_permitted = empty_permitted
self._errors = None # Stores the errors after clean() has been called. self._errors = None # Stores the errors after clean() has been called.
self._changed_data = None
# The base_fields class attribute is the *class-wide* definition of # The base_fields class attribute is the *class-wide* definition of
# fields. Because a particular *instance* of the class might want to # fields. Because a particular *instance* of the class might want to
@ -434,10 +434,9 @@ class BaseForm(object):
""" """
return bool(self.changed_data) return bool(self.changed_data)
@property @cached_property
def changed_data(self): def changed_data(self):
if self._changed_data is None: data = []
self._changed_data = []
for name, field in self.fields.items(): for name, field in self.fields.items():
prefixed_name = self.add_prefix(name) prefixed_name = self.add_prefix(name)
data_value = field.widget.value_from_datadict(self.data, self.files, prefixed_name) data_value = field.widget.value_from_datadict(self.data, self.files, prefixed_name)
@ -453,11 +452,11 @@ class BaseForm(object):
self.data, self.files, initial_prefixed_name)) self.data, self.files, initial_prefixed_name))
except ValidationError: except ValidationError:
# Always assume data has changed if validation fails. # Always assume data has changed if validation fails.
self._changed_data.append(name) data.append(name)
continue continue
if field.has_changed(initial_value, data_value): if field.has_changed(initial_value, data_value):
self._changed_data.append(name) data.append(name)
return self._changed_data return data
@property @property
def media(self): def media(self):