This commit is contained in:
Omer Katz 2014-10-02 15:07:06 +00:00
commit 5c87b6e927
2 changed files with 16 additions and 16 deletions

View File

@ -6,6 +6,7 @@ from django.utils import six, timezone
from django.utils.datastructures import SortedDict from django.utils.datastructures import SortedDict
from django.utils.dateparse import parse_date, parse_datetime, parse_time from django.utils.dateparse import parse_date, parse_datetime, parse_time
from django.utils.encoding import is_protected_type from django.utils.encoding import is_protected_type
from django.utils.functional import cached_property
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from rest_framework import ISO_8601 from rest_framework import ISO_8601
from rest_framework.compat import smart_text, EmailValidator, MinValueValidator, MaxValueValidator, URLValidator from rest_framework.compat import smart_text, EmailValidator, MinValueValidator, MaxValueValidator, URLValidator
@ -296,7 +297,7 @@ class Field(object):
raise AssertionError(msg) raise AssertionError(msg)
raise ValidationError(msg.format(**kwargs)) raise ValidationError(msg.format(**kwargs))
@property @cached_property
def root(self): def root(self):
""" """
Returns the top-level serializer for this field. Returns the top-level serializer for this field.
@ -306,7 +307,7 @@ class Field(object):
root = root.parent root = root.parent
return root return root
@property @cached_property
def context(self): def context(self):
""" """
Returns the context as passed to the root serializer on initialization. Returns the context as passed to the root serializer on initialization.

View File

@ -15,6 +15,7 @@ from django.db import models
from django.utils import six from django.utils import six
from django.utils.datastructures import SortedDict from django.utils.datastructures import SortedDict
from collections import namedtuple from collections import namedtuple
from django.utils.functional import cached_property
from rest_framework.fields import empty, set_value, Field, SkipField from rest_framework.fields import empty, set_value, Field, SkipField
from rest_framework.settings import api_settings from rest_framework.settings import api_settings
from rest_framework.utils import html, model_meta, representation from rest_framework.utils import html, model_meta, representation
@ -108,28 +109,26 @@ class BaseSerializer(Field):
return not bool(self._errors) return not bool(self._errors)
@property @cached_property
def data(self): def data(self):
if not hasattr(self, '_data'): if self.instance is not None:
if self.instance is not None: return self.to_representation(self.instance)
self._data = self.to_representation(self.instance) elif self._initial_data is not None:
elif self._initial_data is not None: return dict([
self._data = dict([ (field_name, field.get_value(self._initial_data))
(field_name, field.get_value(self._initial_data)) for field_name, field in self.fields.items()
for field_name, field in self.fields.items() ])
]) else:
else: return self.get_initial()
self._data = self.get_initial()
return self._data
@property @cached_property
def errors(self): def errors(self):
if not hasattr(self, '_errors'): if not hasattr(self, '_errors'):
msg = 'You must call `.is_valid()` before accessing `.errors`.' msg = 'You must call `.is_valid()` before accessing `.errors`.'
raise AssertionError(msg) raise AssertionError(msg)
return self._errors return self._errors
@property @cached_property
def validated_data(self): def validated_data(self):
if not hasattr(self, '_validated_data'): if not hasattr(self, '_validated_data'):
msg = 'You must call `.is_valid()` before accessing `.validated_data`.' msg = 'You must call `.is_valid()` before accessing `.validated_data`.'