-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Add a compatibility setting for ListSerializer error formats #10027
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| import copy | ||
| import inspect | ||
| import traceback | ||
| import warnings | ||
| from collections import defaultdict | ||
| from collections.abc import Mapping | ||
|
|
||
|
|
@@ -26,6 +27,7 @@ | |
| from django.utils.functional import cached_property | ||
| from django.utils.translation import gettext_lazy as _ | ||
|
|
||
| from rest_framework import RemovedInDRF320Warning | ||
| from rest_framework.compat import postgres_fields | ||
| from rest_framework.exceptions import ErrorDetail, ValidationError | ||
| from rest_framework.fields import get_error_detail | ||
|
|
@@ -709,6 +711,16 @@ def to_internal_value(self, data): | |
| ret.append(validated) | ||
|
|
||
| if errors: | ||
| if not api_settings.LIST_SERIALIZER_ERRORS_AS_DICT: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd say to move this logic to a separate in-line method that you could decorate with Something like this, maybe?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's good to know. Thanks for adding that context! |
||
| warnings.warn( | ||
| 'The list-based error format for `ListSerializer` is ' | ||
| 'deprecated and will be removed in DRF 3.20. Set ' | ||
| '`REST_FRAMEWORK["LIST_SERIALIZER_ERRORS_AS_DICT"]` to ' | ||
| '`True` to use the dictionary-based error format.', | ||
| RemovedInDRF320Warning, | ||
| stacklevel=5 | ||
| ) | ||
| errors = [errors.get(index, {}) for index in range(len(data))] | ||
| raise ValidationError(errors) | ||
|
|
||
| return ret | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,6 +86,7 @@ | |
| # Exception handling | ||
| 'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler', | ||
| 'NON_FIELD_ERRORS_KEY': 'non_field_errors', | ||
| 'LIST_SERIALIZER_ERRORS_AS_DICT': False, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should now default to
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense. I can change that. |
||
|
|
||
| # Testing | ||
| 'TEST_REQUEST_RENDERER_CLASSES': [ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To bring this in line with another PR in flight, please move this class to
rest_framework/deprecation.py