You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api-guide/fields.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -47,7 +47,7 @@ If set, this gives the default value that will be used for the field if no input
47
47
48
48
The `default` is not applied during partial update operations. In the partial update case only fields that are provided in the incoming data will have a validated value returned.
49
49
50
-
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).
50
+
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.
51
51
52
52
When serializing the instance, default will be used if the the object attribute or dictionary key is not present in the instance.
Copy file name to clipboardExpand all lines: docs/api-guide/validators.md
+10-5Lines changed: 10 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -290,13 +290,18 @@ To write a class-based validator, use the `__call__` method. Class-based validat
290
290
message = 'This field must be a multiple of %d.' % self.base
291
291
raise serializers.ValidationError(message)
292
292
293
-
#### Using `set_context()`
293
+
#### Accessing the context
294
294
295
-
In some advanced cases you might want a validator to be passed the serializer field it is being used with as additional context. You can do so by declaring a `set_context` method on a class-based validator.
295
+
In some advanced cases you might want a validator to be passed the serializer
296
+
field it is being used with as additional context. You can do so by using
297
+
`rest_framework.validators.ContextBasedValidator` as a base class for the
298
+
validator. The `__call__` method will then be called with the `serializer_field`
299
+
or `serializer` as an additional argument.
296
300
297
-
def set_context(self, serializer_field):
301
+
def __call__(self, value, serializer_field):
298
302
# Determine if this is an update or a create operation.
299
-
# In `__call__` we can then use that information to modify the validation behavior.
300
-
self.is_update = serializer_field.parent.instance is not None
303
+
is_update = serializer_field.parent.instance is not None
304
+
305
+
pass # implementation of the validator that uses `is_update`
0 commit comments