aboutsummaryrefslogtreecommitdiffstats
path: root/docs/api-guide
diff options
context:
space:
mode:
authorJamie Matthews2012-10-24 11:39:17 +0100
committerJamie Matthews2012-10-24 11:39:17 +0100
commitac2d39892d6b3fbbe5cd53b9ef83367249ba4880 (patch)
tree413fedc79aeedb06722b416fd74af38d767f9c9e /docs/api-guide
parent388a807f64f60d84556288e2ade4f0fe57a8e66b (diff)
downloaddjango-rest-framework-ac2d39892d6b3fbbe5cd53b9ef83367249ba4880.tar.bz2
Add cross-field validate method
Diffstat (limited to 'docs/api-guide')
-rw-r--r--docs/api-guide/serializers.md8
1 files changed, 5 insertions, 3 deletions
diff --git a/docs/api-guide/serializers.md b/docs/api-guide/serializers.md
index 9011d31f..40f8a170 100644
--- a/docs/api-guide/serializers.md
+++ b/docs/api-guide/serializers.md
@@ -76,9 +76,7 @@ Deserialization is similar. First we parse a stream into python native datatype
When deserializing data, you always need to call `is_valid()` before attempting to access the deserialized object. If any validation errors occur, the `.errors` and `.non_field_errors` properties will contain the resulting error messages.
-**TODO: Describe validation in more depth**
-
-## Custom field 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:
@@ -97,6 +95,10 @@ You can specify custom field-level validation by adding `validate_<fieldname>()`
raise serializers.ValidationError("Blog post is not about Django")
return data
+### Final cross-field validation
+
+To do any other validation that requires access to multiple fields, add a method called `validate` to your `Serializer` subclass. This method takes a single argument, which is the `attrs` dictionary. It should raise a `ValidationError` if necessary, or just return `attrs`.
+
## Dealing with nested objects
The previous example is fine for dealing with objects that only have simple datatypes, but sometimes we also need to be able to represent more complex objects,