mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 20:40:14 +03:00
Merge 684eaac7fc
into ffc6aa3abc
This commit is contained in:
commit
5c87b6e927
|
@ -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.
|
||||
|
|
|
@ -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`.'
|
||||
|
|
Loading…
Reference in New Issue
Block a user