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
6fbd23ab
Commit
6fbd23ab
authored
Nov 28, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2086 from beck/doug/blow-up-with-bad-models
Ensure _resolve_model does not return None
parents
8d989bb1
67735687
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
1 deletions
+38
-1
rest_framework/utils/model_meta.py
+5
-1
tests/test_utils.py
+33
-0
No files found.
rest_framework/utils/model_meta.py
View file @
6fbd23ab
...
@@ -43,7 +43,11 @@ def _resolve_model(obj):
...
@@ -43,7 +43,11 @@ def _resolve_model(obj):
"""
"""
if
isinstance
(
obj
,
six
.
string_types
)
and
len
(
obj
.
split
(
'.'
))
==
2
:
if
isinstance
(
obj
,
six
.
string_types
)
and
len
(
obj
.
split
(
'.'
))
==
2
:
app_name
,
model_name
=
obj
.
split
(
'.'
)
app_name
,
model_name
=
obj
.
split
(
'.'
)
return
models
.
get_model
(
app_name
,
model_name
)
resolved_model
=
models
.
get_model
(
app_name
,
model_name
)
if
not
resolved_model
:
raise
ValueError
(
"Django did not return a model for "
"{0}.{1}"
.
format
(
app_name
,
model_name
))
return
resolved_model
elif
inspect
.
isclass
(
obj
)
and
issubclass
(
obj
,
models
.
Model
):
elif
inspect
.
isclass
(
obj
)
and
issubclass
(
obj
,
models
.
Model
):
return
obj
return
obj
raise
ValueError
(
"{0} is not a Django model"
.
format
(
obj
))
raise
ValueError
(
"{0} is not a Django model"
.
format
(
obj
))
...
...
tests/test_utils.py
View file @
6fbd23ab
...
@@ -7,6 +7,8 @@ from rest_framework.utils.breadcrumbs import get_breadcrumbs
...
@@ -7,6 +7,8 @@ from rest_framework.utils.breadcrumbs import get_breadcrumbs
from
rest_framework.views
import
APIView
from
rest_framework.views
import
APIView
from
tests.models
import
BasicModel
from
tests.models
import
BasicModel
import
rest_framework.utils.model_meta
class
Root
(
APIView
):
class
Root
(
APIView
):
pass
pass
...
@@ -130,3 +132,34 @@ class ResolveModelTests(TestCase):
...
@@ -130,3 +132,34 @@ class ResolveModelTests(TestCase):
def
test_resolve_improper_string_representation
(
self
):
def
test_resolve_improper_string_representation
(
self
):
with
self
.
assertRaises
(
ValueError
):
with
self
.
assertRaises
(
ValueError
):
_resolve_model
(
'BasicModel'
)
_resolve_model
(
'BasicModel'
)
class
ResolveModelWithPatchedDjangoTests
(
TestCase
):
"""
Test coverage for when Django's `get_model` returns `None`.
Under certain circumstances Django may return `None` with `get_model`:
http://git.io/get-model-source
It usually happens with circular imports so it is important that DRF
excepts early, otherwise fault happens downstream and is much more
difficult to debug.
"""
def
setUp
(
self
):
"""Monkeypatch get_model."""
self
.
get_model
=
rest_framework
.
utils
.
model_meta
.
models
.
get_model
def
get_model
(
app_label
,
model_name
):
return
None
rest_framework
.
utils
.
model_meta
.
models
.
get_model
=
get_model
def
tearDown
(
self
):
"""Revert monkeypatching."""
rest_framework
.
utils
.
model_meta
.
models
.
get_model
=
self
.
get_model
def
test_blows_up_if_model_does_not_resolve
(
self
):
with
self
.
assertRaises
(
ValueError
):
_resolve_model
(
'tests.BasicModel'
)
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