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
cda74b59
Commit
cda74b59
authored
Feb 27, 2015
by
Tom Christie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2608 from ezheidtmann/dont-swallow-errors-in-callable-sources
Dont swallow errors in callable sources
parents
c66f2339
e6b06c34
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
0 deletions
+32
-0
rest_framework/fields.py
+7
-0
tests/test_fields.py
+25
-0
No files found.
rest_framework/fields.py
View file @
cda74b59
...
@@ -71,7 +71,14 @@ def get_attribute(instance, attrs):
...
@@ -71,7 +71,14 @@ def get_attribute(instance, attrs):
except
ObjectDoesNotExist
:
except
ObjectDoesNotExist
:
return
None
return
None
if
is_simple_callable
(
instance
):
if
is_simple_callable
(
instance
):
try
:
instance
=
instance
()
instance
=
instance
()
except
(
AttributeError
,
KeyError
)
as
exc
:
# If we raised an Attribute or KeyError here it'd get treated
# as an omitted field in `Field.get_attribute()`. Instead we
# raise a ValueError to ensure the exception is not masked.
raise
ValueError
(
'Exception raised in callable attribute "{0}"; original exception was: {1}'
.
format
(
attr
,
exc
))
return
instance
return
instance
...
...
tests/test_fields.py
View file @
cda74b59
...
@@ -93,6 +93,31 @@ class TestSource:
...
@@ -93,6 +93,31 @@ class TestSource:
"same as the field name. Remove the `source` keyword argument."
"same as the field name. Remove the `source` keyword argument."
)
)
def
test_callable_source
(
self
):
class
ExampleSerializer
(
serializers
.
Serializer
):
example_field
=
serializers
.
CharField
(
source
=
'example_callable'
)
class
ExampleInstance
(
object
):
def
example_callable
(
self
):
return
'example callable value'
serializer
=
ExampleSerializer
(
ExampleInstance
())
assert
serializer
.
data
[
'example_field'
]
==
'example callable value'
def
test_callable_source_raises
(
self
):
class
ExampleSerializer
(
serializers
.
Serializer
):
example_field
=
serializers
.
CharField
(
source
=
'example_callable'
,
read_only
=
True
)
class
ExampleInstance
(
object
):
def
example_callable
(
self
):
raise
AttributeError
(
'method call failed'
)
with
pytest
.
raises
(
ValueError
)
as
exc_info
:
serializer
=
ExampleSerializer
(
ExampleInstance
())
serializer
.
data
.
items
()
assert
'method call failed'
in
str
(
exc_info
.
value
)
class
TestReadOnly
:
class
TestReadOnly
:
def
setup
(
self
):
def
setup
(
self
):
...
...
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