Update validators.md

This commit is contained in:
Kenneth Schnall 2016-07-07 15:08:16 -04:00 committed by Ken Schnall
parent 04af9a37d9
commit 844a0e2dab

View File

@ -265,9 +265,9 @@ A validator may be any callable that raises a `serializers.ValidationError` on f
if value % 2 != 0: if value % 2 != 0:
raise serializers.ValidationError('This field must be an even number.') raise serializers.ValidationError('This field must be an even number.')
## Class based ## Class-based
To write a class based validator, use the `__call__` method. Class based validators are useful as they allow you to parameterize and reuse behavior. To write a class-based validator, use the `__call__` method. Class-based validators are useful as they allow you to parameterize and reuse behavior.
class MultipleOf(object): class MultipleOf(object):
def __init__(self, base): def __init__(self, base):
@ -280,7 +280,7 @@ To write a class based validator, use the `__call__` method. Class based validat
#### Using `set_context()` #### Using `set_context()`
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. 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.
def set_context(self, serializer_field): def set_context(self, serializer_field):
# Determine if this is an update or a create operation. # Determine if this is an update or a create operation.