From 21e3b306d8e40d0417c3e0735f3afce0e148830f Mon Sep 17 00:00:00 2001 From: Max Morlocke Date: Wed, 23 Dec 2020 23:22:55 -0500 Subject: [PATCH] provide ValueFromContext documentation --- docs/api-guide/validators.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/api-guide/validators.md b/docs/api-guide/validators.md index 4451489d4..4275b938d 100644 --- a/docs/api-guide/validators.md +++ b/docs/api-guide/validators.md @@ -183,6 +183,16 @@ A default class that can be used to represent the current user. In order to use default=serializers.CurrentUserDefault() ) +#### ValueFromContext + +CurrentUserDefault is an example of extracting a value from context and setting a hidden field to this value. This pattern resurfaces in a number of contexts related to hidden fields, and you may wish to insert something other than user, such as a parent object who's ID was extracted from the URL as part of view processing. Simply pass in the value to extract from the context: + + parent = serializers.HiddenField( + default=serializers.ValueFromContext("parent_object") + ) + +In the event that there is nested data inside the context that you need to extract, you can override ValueFromContext's call method. + #### CreateOnlyDefault A default class that can be used to *only set a default argument during create operations*. During updates the field is omitted.