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
aa571abb
Commit
aa571abb
authored
Dec 13, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Better errors when serializer has incorrectly named field.
parent
eb78da4b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
1 deletions
+38
-1
rest_framework/fields.py
+17
-1
tests/test_serializer.py
+21
-0
No files found.
rest_framework/fields.py
View file @
aa571abb
...
...
@@ -274,7 +274,23 @@ class Field(object):
Given the *outgoing* object instance, return the primitive value
that should be used for this field.
"""
return
get_attribute
(
instance
,
self
.
source_attrs
)
try
:
return
get_attribute
(
instance
,
self
.
source_attrs
)
except
(
KeyError
,
AttributeError
)
as
exc
:
msg
=
(
'Got {exc_type} when attempting to get a value for field '
'`{field}` on serializer `{serializer}`.
\n
The serializer '
'field might be named incorrectly and not match '
'any attribute or key on the `{instance}` instance.
\n
'
'Original exception text was: {exc}.'
.
format
(
exc_type
=
type
(
exc
)
.
__name__
,
field
=
self
.
field_name
,
serializer
=
self
.
parent
.
__class__
.
__name__
,
instance
=
instance
.
__class__
.
__name__
,
exc
=
exc
)
)
raise
type
(
exc
)(
msg
)
def
get_default
(
self
):
"""
...
...
tests/test_serializer.py
View file @
aa571abb
from
__future__
import
unicode_literals
from
rest_framework
import
serializers
import
pytest
...
...
@@ -175,3 +176,23 @@ class TestStarredSource:
instance
=
{
'a'
:
1
,
'b'
:
2
,
'c'
:
3
,
'd'
:
4
}
serializer
=
self
.
Serializer
(
instance
)
assert
serializer
.
data
==
self
.
data
class
TestIncorrectlyConfigured
:
def
test_incorrect_field_name
(
self
):
class
ExampleSerializer
(
serializers
.
Serializer
):
incorrect_name
=
serializers
.
IntegerField
()
class
ExampleObject
:
def
__init__
(
self
):
self
.
correct_name
=
123
instance
=
ExampleObject
()
serializer
=
ExampleSerializer
(
instance
)
with
pytest
.
raises
(
AttributeError
)
as
exc_info
:
serializer
.
data
assert
str
(
exc_info
.
value
)
==
(
"Got AttributeError when attempting to get a value for field `incorrect_name` on serializer `ExampleSerializer`.
\n
"
"The serializer field might be named incorrectly and not match any attribute or key on the `ExampleObject` instance.
\n
"
"Original exception text was: ExampleObject instance has no attribute 'incorrect_name'."
)
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