mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-04-29 05:23:43 +03:00
Simplify Serializer.fields with @cached_property (#6662)
This commit is contained in:
parent
43a9cc1b7a
commit
7232586c7c
|
@ -348,7 +348,7 @@ class Serializer(BaseSerializer, metaclass=SerializerMetaclass):
|
||||||
'invalid': _('Invalid data. Expected a dictionary, but got {datatype}.')
|
'invalid': _('Invalid data. Expected a dictionary, but got {datatype}.')
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def fields(self):
|
def fields(self):
|
||||||
"""
|
"""
|
||||||
A dictionary of {field_name: field_instance}.
|
A dictionary of {field_name: field_instance}.
|
||||||
|
@ -356,11 +356,10 @@ class Serializer(BaseSerializer, metaclass=SerializerMetaclass):
|
||||||
# `fields` is evaluated lazily. We do this to ensure that we don't
|
# `fields` is evaluated lazily. We do this to ensure that we don't
|
||||||
# have issues importing modules that use ModelSerializers as fields,
|
# have issues importing modules that use ModelSerializers as fields,
|
||||||
# even if Django's app-loading stage has not yet run.
|
# even if Django's app-loading stage has not yet run.
|
||||||
if not hasattr(self, '_fields'):
|
fields = BindingDict(self)
|
||||||
self._fields = BindingDict(self)
|
for key, value in self.get_fields().items():
|
||||||
for key, value in self.get_fields().items():
|
fields[key] = value
|
||||||
self._fields[key] = value
|
return fields
|
||||||
return self._fields
|
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def _writable_fields(self):
|
def _writable_fields(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user