Skip to content

Commit ef112f5

Browse files
Provide example for dict in ValidationError detail (#7788)
Added a sentence describing the use of a dictionary as the `detail` argument of a ValidationError, and how it can be used to add field-level errors during object-level validation.
1 parent ec29ff8 commit ef112f5

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

docs/api-guide/exceptions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ By default this exception results in a response with the HTTP status code "429 T
222222
The `ValidationError` exception is slightly different from the other `APIException` classes:
223223

224224
* The `detail` argument is mandatory, not optional.
225-
* The `detail` argument may be a list or dictionary of error details, and may also be a nested data structure.
225+
* The `detail` argument may be a list or dictionary of error details, and may also be a nested data structure. By using a dictionary, you can specify field-level errors while performing object-level validation in the `validate()` method of a serializer. For example. `raise serializers.ValidationError({'name': 'Please enter a valid name.'})`
226226
* By convention you should import the serializers module and use a fully qualified `ValidationError` style, in order to differentiate it from Django's built-in validation error. For example. `raise serializers.ValidationError('This field must be an integer value.')`
227227

228228
The `ValidationError` class should be used for serializer and field validation, and by validator classes. It is also raised when calling `serializer.is_valid` with the `raise_exception` keyword argument:

0 commit comments

Comments
 (0)