Browse Source

get_model: Raise LookupError instead of ImportError

oscar.core.loading.get_model is a thin wrapper around Django 1.6's
get_model function. The latter returns None if the model can't be found,
whereas the Oscar variant loudly failed with an ImportError.
With Django's app refactor, most of that behaviour in Django has been
changed, and Oscar's get_model falls back to Django's
get_registered_model. But the latter raises a LookupError.

To avoid having to catch both errors, Oscar's get_model now raises a
LookupError no matter what Django version is used. This should have
minimal impact on backwards compatibility, as get_model isn't really
used outside of Oscar core.
master
Maik Hoepfel 11 years ago
parent
commit
38f03e1865
2 changed files with 12 additions and 5 deletions
  1. 4
    0
      docs/source/releases/v0.8.rst
  2. 8
    5
      oscar/core/loading.py

+ 4
- 0
docs/source/releases/v0.8.rst View File

@@ -455,6 +455,10 @@ Misc
455 455
   issue, as the model is only used while an range upload is in progress. If
456 456
   you need to keep the data, ensure you migrate it across.
457 457
 
458
+* ``oscar.core.loading.get_model`` now raises a ``LookupError`` instead of an
459
+  ``ImportError`` if a model can't be found. That brings it more in line with
460
+  what Django does since the app refactor.
461
+
458 462
 .. _rewritten: https://github.com/tangentlabs/django-oscar/commit/d8b4dbfed17be90846ea4bc47b5f7b39ad944c24
459 463
 
460 464
 Basket line stockrecords

+ 8
- 5
oscar/core/loading.py View File

@@ -260,10 +260,11 @@ def feature_hidden(feature_name):
260 260
 
261 261
 
262 262
 # The following section is concerned with offering both the
263
-# get_model(app_label, model_name) and is_model_registered(app_label, model_name)
264
-# methods. Because the Django internals dramatically changed in the Django 1.7
265
-# app refactor, we distinguish based on the Django version and declare
266
-# a total of four methods that hopefully do mostly the same
263
+# get_model(app_label, model_name) and
264
+# is_model_registered(app_label, model_name) methods. Because the Django
265
+# internals dramatically changed in the Django 1.7 app refactor, we distinguish
266
+# based on the Django version and declare a total of four methods that
267
+# hopefully do mostly the same
267 268
 
268 269
 
269 270
 if django.VERSION < (1, 7):
@@ -275,10 +276,11 @@ if django.VERSION < (1, 7):
275 276
         Gets a model class by it's app label and model name. Fails loudly if
276 277
         the model class can't be imported.
277 278
         This is merely a thin wrapper around Django's get_model function.
279
+        Raises LookupError if model isn't found.
278 280
         """
279 281
         model = django_get_model(app_label, model_name, *args, **kwargs)
280 282
         if model is None:
281
-            raise ImportError(
283
+            raise LookupError(
282 284
                 "{app_label}.{model_name} could not be imported.".format(
283 285
                     app_label=app_label, model_name=model_name))
284 286
         return model
@@ -302,6 +304,7 @@ else:
302 304
         which makes it safe to call when the registry is being populated.
303 305
         All other methods to access models might raise an exception about the
304 306
         registry not being ready yet.
307
+        Raises LookupError if model isn't found.
305 308
         """
306 309
         return apps.get_registered_model(app_label, model_name)
307 310
 

Loading…
Cancel
Save