From 1c049edd8cc7da265deee47eb6ef004fcb297d21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Thu, 3 Sep 2015 09:43:27 +0200 Subject: [PATCH 1/4] Improve documentation of default argument for fields The documentation now not only mentions that callables are acceptable arguments, it also tells the reader what the callable should look like. --- docs/api-guide/fields.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api-guide/fields.md b/docs/api-guide/fields.md index 2b9a6bba6..f681d9453 100644 --- a/docs/api-guide/fields.md +++ b/docs/api-guide/fields.md @@ -51,7 +51,7 @@ Defaults to `False` If set, this gives the default value that will be used for the field if no input value is supplied. If not set the default behavior is to not populate the attribute at all. -May be set to a function or other callable, in which case the value will be evaluated each time it is used. +May be set to a function or other callable, in which case the value will be evaluated each time it is used. When called, it will receive no arguments. If the callable has a `set_context` method, that will be called each time before getting the value with the field instance as only argument. Note that setting a `default` value implies that the field is not required. Including both the `default` and `required` keyword arguments is invalid and will raise an error. From a57ad0767421a86bd35bf5a72c337d6857ad5f42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubom=C3=ADr=20Sedl=C3=A1=C5=99?= Date: Thu, 3 Sep 2015 10:35:28 +0200 Subject: [PATCH 2/4] Add link to validators The `set_context` method there is the same as for the default field. --- docs/api-guide/fields.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api-guide/fields.md b/docs/api-guide/fields.md index f681d9453..2fbb84c03 100644 --- a/docs/api-guide/fields.md +++ b/docs/api-guide/fields.md @@ -51,7 +51,7 @@ Defaults to `False` If set, this gives the default value that will be used for the field if no input value is supplied. If not set the default behavior is to not populate the attribute at all. -May be set to a function or other callable, in which case the value will be evaluated each time it is used. When called, it will receive no arguments. If the callable has a `set_context` method, that will be called each time before getting the value with the field instance as only argument. +May be set to a function or other callable, in which case the value will be evaluated each time it is used. When called, it will receive no arguments. If the callable has a `set_context` method, that will be called each time before getting the value with the field instance as only argument. This works the same way as for [validators](validators.md#using-set_context). Note that setting a `default` value implies that the field is not required. Including both the `default` and `required` keyword arguments is invalid and will raise an error. From 671de792e203a9f0165a6f943c69427c55ef37fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D=C3=ADaz?= Date: Thu, 10 Sep 2015 10:19:40 +0300 Subject: [PATCH 3/4] Fixed typo in ScopedRateThrottle example views --- docs/api-guide/throttling.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api-guide/throttling.md b/docs/api-guide/throttling.md index 3f668867c..57bce54e7 100644 --- a/docs/api-guide/throttling.md +++ b/docs/api-guide/throttling.md @@ -148,7 +148,7 @@ For example, given the following views... throttle_scope = 'contacts' ... - class ContactDetailView(ApiView): + class ContactDetailView(APIView): throttle_scope = 'contacts' ... From 4704da9a1acf3cbf1b1b1396821fa6ee61e0ffce Mon Sep 17 00:00:00 2001 From: Nic Young Date: Thu, 10 Sep 2015 17:17:33 -0700 Subject: [PATCH 4/4] Add note about field level validation, fixes #3306 The documentation added warns the user that field level validation will be skipped if the field is declared on the model with the parameter `required=False`. --- docs/api-guide/serializers.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/api-guide/serializers.md b/docs/api-guide/serializers.md index abdb67afa..1ac845901 100644 --- a/docs/api-guide/serializers.md +++ b/docs/api-guide/serializers.md @@ -189,6 +189,12 @@ Your `validate_` methods should return the validated value or raise raise serializers.ValidationError("Blog post is not about Django") return value +--- + +**Note:** If your `` is declared on your serializer with the parameter `required=False` then this validation step will not take place if the field is not included. + +--- + #### Object-level 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 a dictionary of field values. It should raise a `ValidationError` if necessary, or just return the validated values. For example: