Fix some bits of serialization

This commit is contained in:
Tom Christie 2012-09-25 13:20:12 +01:00
parent 4fb57d28e6
commit e0913e29b8
3 changed files with 10 additions and 8 deletions

View File

@ -42,7 +42,11 @@ class Field(object):
self.source = source
self.readonly = readonly
self.required = not(readonly)
if required is None:
self.required = not(readonly)
else:
assert not readonly, "Cannot set required=True and readonly=True"
self.required = required
messages = {}
for c in reversed(self.__class__.__mro__):
@ -66,9 +70,8 @@ class Field(object):
self.model_field = model_field
def validate(self, value):
pass
# if value in validators.EMPTY_VALUES and self.required:
# raise ValidationError(self.error_messages['required'])
if value in validators.EMPTY_VALUES and self.required:
raise ValidationError(self.error_messages['required'])
def run_validators(self, value):
if value in validators.EMPTY_VALUES:

View File

@ -95,7 +95,7 @@ class BaseSerializer(Field):
self.context = context or {}
self.init_data = data
self.instance = instance
self.object = instance
self._data = None
self._errors = None
@ -247,7 +247,7 @@ class BaseSerializer(Field):
self._errors = {}
attrs = self.restore_fields(data)
if not self._errors:
return self.restore_object(attrs, instance=getattr(self, 'instance', None))
return self.restore_object(attrs, instance=getattr(self, 'object', None))
@property
def errors(self):
@ -267,7 +267,7 @@ class BaseSerializer(Field):
@property
def data(self):
if self._data is None:
self._data = self.to_native(self.instance)
self._data = self.to_native(self.object)
return self._data

View File

@ -103,7 +103,6 @@ def add_class(value, css_class):
m = re.search(r'^%s$|^%s\s|\s%s\s|\s%s$' % (css_class, css_class,
css_class, css_class),
match.group(1))
print match.group(1)
if not m:
return mark_safe(class_re.sub(match.group(1) + " " + css_class,
html))