Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
6db3356c
Commit
6db3356c
authored
Sep 12, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
NON_FIELD_ERRORS_KEY setting
parent
0d354e8f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
14 additions
and
3 deletions
+14
-3
rest_framework/serializers.py
+6
-2
rest_framework/settings.py
+1
-0
rest_framework/views.py
+7
-1
No files found.
rest_framework/serializers.py
View file @
6db3356c
...
@@ -198,7 +198,9 @@ class Serializer(BaseSerializer):
...
@@ -198,7 +198,9 @@ class Serializer(BaseSerializer):
Dict of native values <- Dict of primitive datatypes.
Dict of native values <- Dict of primitive datatypes.
"""
"""
if
not
isinstance
(
data
,
dict
):
if
not
isinstance
(
data
,
dict
):
raise
ValidationError
({
'non_field_errors'
:
[
'Invalid data'
]})
raise
ValidationError
({
api_settings
.
NON_FIELD_ERRORS_KEY
:
[
'Invalid data'
]
})
ret
=
{}
ret
=
{}
errors
=
{}
errors
=
{}
...
@@ -224,7 +226,9 @@ class Serializer(BaseSerializer):
...
@@ -224,7 +226,9 @@ class Serializer(BaseSerializer):
try
:
try
:
return
self
.
validate
(
ret
)
return
self
.
validate
(
ret
)
except
ValidationError
as
exc
:
except
ValidationError
as
exc
:
raise
ValidationError
({
'non_field_errors'
:
exc
.
messages
})
raise
ValidationError
({
api_settings
.
NON_FIELD_ERRORS_KEY
:
exc
.
messages
})
def
to_representation
(
self
,
instance
):
def
to_representation
(
self
,
instance
):
"""
"""
...
...
rest_framework/settings.py
View file @
6db3356c
...
@@ -77,6 +77,7 @@ DEFAULTS = {
...
@@ -77,6 +77,7 @@ DEFAULTS = {
# Exception handling
# Exception handling
'EXCEPTION_HANDLER'
:
'rest_framework.views.exception_handler'
,
'EXCEPTION_HANDLER'
:
'rest_framework.views.exception_handler'
,
'NON_FIELD_ERRORS_KEY'
:
'non_field_errors'
,
# Testing
# Testing
'TEST_REQUEST_RENDERER_CLASSES'
:
(
'TEST_REQUEST_RENDERER_CLASSES'
:
(
...
...
rest_framework/views.py
View file @
6db3356c
...
@@ -3,7 +3,7 @@ Provides an APIView class that is the base of all views in REST framework.
...
@@ -3,7 +3,7 @@ Provides an APIView class that is the base of all views in REST framework.
"""
"""
from
__future__
import
unicode_literals
from
__future__
import
unicode_literals
from
django.core.exceptions
import
PermissionDenied
,
ValidationError
from
django.core.exceptions
import
PermissionDenied
,
ValidationError
,
NON_FIELD_ERRORS
from
django.http
import
Http404
from
django.http
import
Http404
from
django.utils.datastructures
import
SortedDict
from
django.utils.datastructures
import
SortedDict
from
django.views.decorators.csrf
import
csrf_exempt
from
django.views.decorators.csrf
import
csrf_exempt
...
@@ -69,6 +69,12 @@ def exception_handler(exc):
...
@@ -69,6 +69,12 @@ def exception_handler(exc):
headers
=
headers
)
headers
=
headers
)
elif
isinstance
(
exc
,
ValidationError
):
elif
isinstance
(
exc
,
ValidationError
):
# ValidationErrors may include the non-field key named '__all__'.
# When returning a response we map this to a key name that can be
# modified in settings.
if
NON_FIELD_ERRORS
in
exc
.
message_dict
:
errors
=
exc
.
message_dict
.
pop
(
NON_FIELD_ERRORS
)
exc
.
message_dict
[
api_settings
.
NON_FIELD_ERRORS_KEY
]
=
errors
return
Response
(
exc
.
message_dict
,
return
Response
(
exc
.
message_dict
,
status
=
status
.
HTTP_400_BAD_REQUEST
)
status
=
status
.
HTTP_400_BAD_REQUEST
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment