Made Field.error_messages a cached property.

This speeds up field creation and reduces memory usage.
This commit is contained in:
Collin Anderson 2022-02-16 14:30:04 -05:00 committed by GitHub
parent 97d7990abd
commit 35c2474f16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 5 deletions

View File

@ -217,12 +217,7 @@ class Field(RegisterLookupMixin):
self._validators = list(validators) # Store for deconstruction later
messages = {}
for c in reversed(self.__class__.__mro__):
messages.update(getattr(c, "default_error_messages", {}))
messages.update(error_messages or {})
self._error_messages = error_messages # Store for deconstruction later
self.error_messages = messages
def __str__(self):
"""
@ -669,6 +664,14 @@ class Field(RegisterLookupMixin):
"""
return value
@cached_property
def error_messages(self):
messages = {}
for c in reversed(self.__class__.__mro__):
messages.update(getattr(c, "default_error_messages", {}))
messages.update(self._error_messages or {})
return messages
@cached_property
def validators(self):
"""