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
003c42b0
Commit
003c42b0
authored
Nov 03, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use invalid_data key for error message. Closes #2002.
parent
d27b8cc0
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
3 deletions
+14
-3
rest_framework/serializers.py
+9
-1
tests/test_validation.py
+5
-2
No files found.
rest_framework/serializers.py
View file @
003c42b0
...
...
@@ -15,6 +15,7 @@ from django.db import models
from
django.db.models.fields
import
FieldDoesNotExist
from
django.utils
import
six
from
django.utils.datastructures
import
SortedDict
from
django.utils.translation
import
ugettext_lazy
as
_
from
rest_framework.exceptions
import
ValidationError
from
rest_framework.fields
import
empty
,
set_value
,
Field
,
SkipField
from
rest_framework.settings
import
api_settings
...
...
@@ -282,6 +283,10 @@ class SerializerMetaclass(type):
@six.add_metaclass
(
SerializerMetaclass
)
class
Serializer
(
BaseSerializer
):
default_error_messages
=
{
'invalid'
:
_
(
'Invalid data. Expected a dictionary, but got {datatype}.'
)
}
@property
def
fields
(
self
):
if
not
hasattr
(
self
,
'_fields'
):
...
...
@@ -339,8 +344,11 @@ class Serializer(BaseSerializer):
return
None
if
not
isinstance
(
data
,
dict
):
message
=
self
.
error_messages
[
'invalid'
]
.
format
(
datatype
=
type
(
data
)
.
__name__
)
raise
ValidationError
({
api_settings
.
NON_FIELD_ERRORS_KEY
:
[
'Invalid data'
]
api_settings
.
NON_FIELD_ERRORS_KEY
:
[
message
]
})
value
=
self
.
to_internal_value
(
data
)
...
...
tests/test_validation.py
View file @
003c42b0
...
...
@@ -87,8 +87,11 @@ class TestAvoidValidation(TestCase):
def
test_serializer_errors_has_only_invalid_data_error
(
self
):
serializer
=
ValidationSerializer
(
data
=
'invalid data'
)
self
.
assertFalse
(
serializer
.
is_valid
())
self
.
assertDictEqual
(
serializer
.
errors
,
{
'non_field_errors'
:
[
'Invalid data'
]})
self
.
assertDictEqual
(
serializer
.
errors
,
{
'non_field_errors'
:
[
'Invalid data. Expected a dictionary, but got unicode.'
]
})
# regression tests for issue: 1493
...
...
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