diff options
| author | Jamie Matthews | 2012-10-24 11:27:01 +0100 |
|---|---|---|
| committer | Jamie Matthews | 2012-10-24 11:27:01 +0100 |
| commit | 388a807f64f60d84556288e2ade4f0fe57a8e66b (patch) | |
| tree | c7b376e48df0e781e195592b625e3fcd940c2e5f /docs/api-guide/serializers.md | |
| parent | 51fae73f3d565e2702c72ff9841cc072d6490804 (diff) | |
| download | django-rest-framework-388a807f64f60d84556288e2ade4f0fe57a8e66b.tar.bz2 | |
Switch from clean_<fieldname> to validate_<fieldname>, clarify documentation
Diffstat (limited to 'docs/api-guide/serializers.md')
| -rw-r--r-- | docs/api-guide/serializers.md | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/docs/api-guide/serializers.md b/docs/api-guide/serializers.md index e1e12e74..9011d31f 100644 --- a/docs/api-guide/serializers.md +++ b/docs/api-guide/serializers.md @@ -80,19 +80,21 @@ When deserializing data, you always need to call `is_valid()` before attempting ## Custom field validation -Like Django forms, you can specify custom field-level validation by adding `clean_<fieldname>()` methods to your `Serializer` subclass. This method takes 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, if one was provided.) It should either 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 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: - class BlogPostSerializer(Serializer): + from rest_framework import serializers + + class BlogPostSerializer(serializers.Serializer): title = serializers.CharField(max_length=100) content = serializers.CharField() - def clean_title(self, data, source): + def validate_title(self, data, source): """ Check that the blog post is about Django """ value = data[source] if "Django" not in value: - raise ValidationError("Blog post is not about Django") + raise serializers.ValidationError("Blog post is not about Django") return data ## Dealing with nested objects |
