From 684eaac7fce0557e14f11c12bf5be71acb2e190b Mon Sep 17 00:00:00 2001 From: Omer Katz Date: Thu, 2 Oct 2014 18:00:59 +0300 Subject: [PATCH] Used the cached_property decorator on the serializer object and the field object. --- rest_framework/fields.py | 5 +++-- rest_framework/serializers.py | 27 +++++++++++++-------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index f3ff22334..6ed7f786d 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -6,6 +6,7 @@ from django.utils import six, timezone from django.utils.datastructures import SortedDict from django.utils.dateparse import parse_date, parse_datetime, parse_time from django.utils.encoding import is_protected_type +from django.utils.functional import cached_property from django.utils.translation import ugettext_lazy as _ from rest_framework import ISO_8601 from rest_framework.compat import smart_text, EmailValidator, MinValueValidator, MaxValueValidator, URLValidator @@ -296,7 +297,7 @@ class Field(object): raise AssertionError(msg) raise ValidationError(msg.format(**kwargs)) - @property + @cached_property def root(self): """ Returns the top-level serializer for this field. @@ -306,7 +307,7 @@ class Field(object): root = root.parent return root - @property + @cached_property def context(self): """ Returns the context as passed to the root serializer on initialization. diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 5da812477..bcd1ecdc6 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -15,6 +15,7 @@ from django.db import models from django.utils import six from django.utils.datastructures import SortedDict from collections import namedtuple +from django.utils.functional import cached_property from rest_framework.fields import empty, set_value, Field, SkipField from rest_framework.settings import api_settings from rest_framework.utils import html, model_meta, representation @@ -108,28 +109,26 @@ class BaseSerializer(Field): return not bool(self._errors) - @property + @cached_property def data(self): - if not hasattr(self, '_data'): - if self.instance is not None: - self._data = self.to_representation(self.instance) - elif self._initial_data is not None: - self._data = dict([ - (field_name, field.get_value(self._initial_data)) - for field_name, field in self.fields.items() - ]) - else: - self._data = self.get_initial() - return self._data + if self.instance is not None: + return self.to_representation(self.instance) + elif self._initial_data is not None: + return dict([ + (field_name, field.get_value(self._initial_data)) + for field_name, field in self.fields.items() + ]) + else: + return self.get_initial() - @property + @cached_property def errors(self): if not hasattr(self, '_errors'): msg = 'You must call `.is_valid()` before accessing `.errors`.' raise AssertionError(msg) return self._errors - @property + @cached_property def validated_data(self): if not hasattr(self, '_validated_data'): msg = 'You must call `.is_valid()` before accessing `.validated_data`.'