File tree Expand file tree Collapse file tree 2 files changed +38
-1
lines changed Expand file tree Collapse file tree 2 files changed +38
-1
lines changed Original file line number Diff line number Diff line change @@ -43,7 +43,11 @@ def _resolve_model(obj):
43
43
"""
44
44
if isinstance (obj , six .string_types ) and len (obj .split ('.' )) == 2 :
45
45
app_name , model_name = obj .split ('.' )
46
- return models .get_model (app_name , model_name )
46
+ resolved_model = models .get_model (app_name , model_name )
47
+ if not resolved_model :
48
+ raise ValueError ("Django did not return a model for "
49
+ "{0}.{1}" .format (app_name , model_name ))
50
+ return resolved_model
47
51
elif inspect .isclass (obj ) and issubclass (obj , models .Model ):
48
52
return obj
49
53
raise ValueError ("{0} is not a Django model" .format (obj ))
Original file line number Diff line number Diff line change 7
7
from rest_framework .views import APIView
8
8
from tests .models import BasicModel
9
9
10
+ import rest_framework .utils .model_meta
11
+
10
12
11
13
class Root (APIView ):
12
14
pass
@@ -130,3 +132,34 @@ def test_resolve_non_django_model(self):
130
132
def test_resolve_improper_string_representation (self ):
131
133
with self .assertRaises (ValueError ):
132
134
_resolve_model ('BasicModel' )
135
+
136
+
137
+ class ResolveModelWithPatchedDjangoTests (TestCase ):
138
+ """
139
+ Test coverage for when Django's `get_model` returns `None`.
140
+
141
+ Under certain circumstances Django may return `None` with `get_model`:
142
+ http://git.io/get-model-source
143
+
144
+ It usually happens with circular imports so it is important that DRF
145
+ excepts early, otherwise fault happens downstream and is much more
146
+ difficult to debug.
147
+
148
+ """
149
+
150
+ def setUp (self ):
151
+ """Monkeypatch get_model."""
152
+ self .get_model = rest_framework .utils .model_meta .models .get_model
153
+
154
+ def get_model (app_label , model_name ):
155
+ return None
156
+
157
+ rest_framework .utils .model_meta .models .get_model = get_model
158
+
159
+ def tearDown (self ):
160
+ """Revert monkeypatching."""
161
+ rest_framework .utils .model_meta .models .get_model = self .get_model
162
+
163
+ def test_blows_up_if_model_does_not_resolve (self ):
164
+ with self .assertRaises (ValueError ):
165
+ _resolve_model ('tests.BasicModel' )
You can’t perform that action at this time.
0 commit comments