Clean up internal names and documentation

This commit is contained in:
Jamie Matthews 2012-10-24 11:43:30 +01:00
parent ac2d39892d
commit d60d598e02
2 changed files with 12 additions and 12 deletions

View File

@ -78,7 +78,7 @@ When deserializing data, you always need to call `is_valid()` before attempting
### Field-level validation ### Field-level validation
You can specify custom field-level validation by adding `validate_<fieldname>()` methods to your `Serializer` subclass. These are analagous to `clean_<fieldname>` methods on Django forms, but accept slightly different arguments. They take a dictionary of deserialized data as a first argument, and the field name in that data as a second argument (which will be either the name of the field or the value of the `source` argument to the field, if one was provided). Your `validate_<fieldname>` methods should either just return the data dictionary or raise a `ValidationError`. For example: You can specify custom field-level validation by adding `validate_<fieldname>()` methods to your `Serializer` subclass. These are analagous to `clean_<fieldname>` methods on Django forms, but accept slightly different arguments. They take a dictionary of deserialized attributes as a first argument, and the field name in that dictionary as a second argument (which will be either the name of the field or the value of the `source` argument to the field, if one was provided). Your `validate_<fieldname>` methods should either just return the attrs dictionary or raise a `ValidationError`. For example:
from rest_framework import serializers from rest_framework import serializers
@ -86,14 +86,14 @@ You can specify custom field-level validation by adding `validate_<fieldname>()`
title = serializers.CharField(max_length=100) title = serializers.CharField(max_length=100)
content = serializers.CharField() content = serializers.CharField()
def validate_title(self, data, source): def validate_title(self, attrs, source):
""" """
Check that the blog post is about Django Check that the blog post is about Django
""" """
value = data[source] value = attrs[source]
if "Django" not in value: if "Django" not in value:
raise serializers.ValidationError("Blog post is not about Django") raise serializers.ValidationError("Blog post is not about Django")
return data return attrs
### Final cross-field validation ### Final cross-field validation

View File

@ -208,24 +208,24 @@ class BaseSerializer(Field):
return reverted_data return reverted_data
def clean_fields(self, data): def validate_fields(self, attrs):
""" """
Run validate_<fieldname> methods on the serializer Run validate_<fieldname> methods on the serializer
""" """
fields = self.get_fields(serialize=False, data=data, nested=self.opts.nested) fields = self.get_fields(serialize=False, data=attrs, nested=self.opts.nested)
for field_name, field in fields.items(): for field_name, field in fields.items():
try: try:
clean_method = getattr(self, 'validate_%s' % field_name, None) clean_method = getattr(self, 'validate_%s' % field_name, None)
if clean_method: if clean_method:
source = field.source or field_name source = field.source or field_name
data = clean_method(data, source) attrs = clean_method(attrs, source)
except ValidationError as err: except ValidationError as err:
self._errors[field_name] = self._errors.get(field_name, []) + list(err.messages) self._errors[field_name] = self._errors.get(field_name, []) + list(err.messages)
return data return attrs
def clean_all(self, attrs): def validate_all(self, attrs):
""" """
Run the `validate` method on the serializer, if it exists Run the `validate` method on the serializer, if it exists
""" """
@ -270,10 +270,10 @@ class BaseSerializer(Field):
self._errors = {} self._errors = {}
if data is not None: if data is not None:
attrs = self.restore_fields(data) attrs = self.restore_fields(data)
attrs = self.clean_fields(attrs) attrs = self.validate_fields(attrs)
attrs = self.clean_all(attrs) attrs = self.validate_all(attrs)
else: else:
self._errors['non_field_errors'] = 'No input provided' self._errors['non_field_errors'] = ['No input provided']
if not self._errors: if not self._errors:
return self.restore_object(attrs, instance=getattr(self, 'object', None)) return self.restore_object(attrs, instance=getattr(self, 'object', None))