Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Changed get_model to raise an exception on errors.

Returning None on errors required unpythonic error checking and was
inconsistent with get_app_config.

get_model was a private API until the previous commit, but given that it
was certainly used in third party software, the change is explained in
the release notes.

Applied the same change to get_registered_model, which is a new private
API introduced during the recent refactoring.
  • Loading branch information...
commit ba7206cd81458865bace85914905392291b05829 1 parent 54790e6
@aaugustin aaugustin authored
View
17 django/apps/registry.py
@@ -248,14 +248,11 @@ def get_model(self, app_label, model_name):
model_name is case-insensitive.
- Returns None if no application exists with this label, or no model
- exists with this name in the application.
+ Raises LookupError if no application exists with this label, or no
+ model exists with this name in the application.
"""
self.populate_models()
- try:
- return self.get_app_config(app_label).get_model(model_name.lower())
- except LookupError:
- return None
+ return self.get_app_config(app_label).get_model(model_name.lower())
def register_model(self, app_label, model):
# Since this method is called when models are imported, it cannot
@@ -289,9 +286,13 @@ def get_registered_model(self, app_label, model_name):
the given app_label.
It's safe to call this method at import time, even while the registry
- is being populated. It returns None for models that aren't loaded yet.
+ is being populated.
"""
- return self.all_models[app_label].get(model_name.lower())
+ model = self.all_models[app_label].get(model_name.lower())
+ if model is None:
+ raise LookupError(
+ "Model '%s.%s' not registered." % (app_label, model_name))
+ return model
def set_available_apps(self, available):
"""
View
5 django/contrib/admindocs/views.py
@@ -188,8 +188,9 @@ def get_context_data(self, **kwargs):
apps.get_app_config(self.kwargs['app_label'])
except LookupError:
raise Http404(_("App %(app_label)r not found") % self.kwargs)
- model = apps.get_model(self.kwargs['app_label'], self.kwargs['model_name'])
- if model is None:
+ try:
+ model = apps.get_model(self.kwargs['app_label'], self.kwargs['model_name'])
+ except LookupError:
raise Http404(_("Model %(model_name)r not found in app %(app_label)r") % self.kwargs)
opts = model._meta
View
5 django/contrib/auth/__init__.py
@@ -129,8 +129,9 @@ def get_user_model():
app_label, model_name = settings.AUTH_USER_MODEL.split('.')
except ValueError:
raise ImproperlyConfigured("AUTH_USER_MODEL must be of the form 'app_label.model_name'")
- user_model = apps.get_model(app_label, model_name)
- if user_model is None:
+ try:
+ user_model = apps.get_model(app_label, model_name)
+ except LookupError:
raise ImproperlyConfigured("AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL)
return user_model
View
8 django/contrib/auth/management/__init__.py
@@ -61,7 +61,9 @@ def _check_permission_clashing(custom, builtin, ctype):
def create_permissions(app, created_models, verbosity, db=DEFAULT_DB_ALIAS, **kwargs):
- if apps.get_model('auth', 'Permission') is None:
+ try:
+ apps.get_model('auth', 'Permission')
+ except LookupError:
return
if not router.allow_migrate(db, auth_app.Permission):
@@ -117,7 +119,9 @@ def create_permissions(app, created_models, verbosity, db=DEFAULT_DB_ALIAS, **kw
def create_superuser(app, created_models, verbosity, db, **kwargs):
- if apps.get_model('auth', 'Permission') is None:
+ try:
+ apps.get_model('auth', 'Permission')
+ except LookupError:
return
UserModel = get_user_model()
View
2  django/contrib/comments/views/comments.py
@@ -54,7 +54,7 @@ def post_comment(request, next=None, using=None):
except TypeError:
return CommentPostBadRequest(
"Invalid content_type value: %r" % escape(ctype))
- except AttributeError:
+ except LookupError:
return CommentPostBadRequest(
"The given content-type %r does not resolve to a valid model." % \
escape(ctype))
View
4 django/contrib/contenttypes/management.py
@@ -12,7 +12,9 @@ def update_contenttypes(app, created_models, verbosity=2, db=DEFAULT_DB_ALIAS, *
Creates content types for models in the given app, removing any model
entries that no longer have a matching model class.
"""
- if apps.get_model('contenttypes', 'ContentType') is None:
+ try:
+ apps.get_model('contenttypes', 'ContentType')
+ except LookupError:
return
if not router.allow_migrate(db, ContentType):
View
5 django/contrib/contenttypes/models.py
@@ -157,7 +157,10 @@ def __str__(self):
def model_class(self):
"Returns the Python model class for this type of content."
- return apps.get_model(self.app_label, self.model)
+ try:
+ return apps.get_model(self.app_label, self.model)
+ except LookupError:
+ return None
def get_object_for_this_type(self, **kwargs):
"""
View
5 django/contrib/gis/sitemaps/views.py
@@ -81,8 +81,9 @@ def kml(request, label, model, field_name=None, compress=False, using=DEFAULT_DB
must be that of a geographic field.
"""
placemarks = []
- klass = apps.get_model(label, model)
- if not klass:
+ try:
+ klass = apps.get_model(label, model)
+ except LookupError:
raise Http404('You must supply a valid app label and module name. Got "%s.%s"' % (label, model))
if field_name:
View
10 django/core/management/commands/dumpdata.py
@@ -66,8 +66,9 @@ def handle(self, *app_labels, **options):
for exclude in excludes:
if '.' in exclude:
app_label, model_name = exclude.split('.', 1)
- model = apps.get_model(app_label, model_name)
- if not model:
+ try:
+ model = apps.get_model(app_label, model_name)
+ except LookupError:
raise CommandError('Unknown model in excludes: %s' % exclude)
excluded_models.add(model)
else:
@@ -96,8 +97,9 @@ def handle(self, *app_labels, **options):
raise CommandError("Unknown application: %s" % app_label)
if app_config.models_module is None or app_config in excluded_apps:
continue
- model = apps.get_model(app_label, model_label)
- if model is None:
+ try:
+ model = apps.get_model(app_label, model_label)
+ except LookupError:
raise CommandError("Unknown model: %s.%s" % (app_label, model_label))
app_list_value = app_list.setdefault(app_config, [])
View
4 django/core/management/validation.py
@@ -42,7 +42,9 @@ def get_validation_errors(outfile, app=None):
except ValueError:
e.add(opts, "%s is not of the form 'app_label.app_name'." % opts.swappable)
continue
- if not apps.get_model(app_label, model_name):
+ try:
+ apps.get_model(app_label, model_name)
+ except LookupError:
e.add(opts, "Model has been swapped out for '%s' which has not been installed or is abstract." % opts.swapped)
# No need to perform any other validation checks on a swapped model.
continue
View
4 django/core/serializers/python.py
@@ -156,8 +156,6 @@ def _get_model(model_identifier):
"""
try:
Model = apps.get_model(*model_identifier.split("."))
- except TypeError:
- Model = None
- if Model is None:
+ except (LookupError, TypeError):
raise base.DeserializationError("Invalid model identifier: '%s'" % model_identifier)
return Model
View
4 django/core/serializers/xml_serializer.py
@@ -278,9 +278,7 @@ def _get_model_from_node(self, node, attr):
% (node.nodeName, attr))
try:
Model = apps.get_model(*model_identifier.split("."))
- except TypeError:
- Model = None
- if Model is None:
+ except (LookupError, TypeError):
raise base.DeserializationError(
"<%s> node has invalid model identifier: '%s'"
% (node.nodeName, model_identifier))
View
11 django/db/migrations/state.py
@@ -192,11 +192,12 @@ def render(self, apps):
meta_contents["unique_together"] = list(meta_contents["unique_together"])
meta = type("Meta", tuple(), meta_contents)
# Then, work out our bases
- bases = tuple(
- (apps.get_model(*base.split(".", 1)) if isinstance(base, six.string_types) else base)
- for base in self.bases
- )
- if None in bases:
+ try:
+ bases = tuple(
+ (apps.get_model(*base.split(".", 1)) if isinstance(base, six.string_types) else base)
+ for base in self.bases
+ )
+ except LookupError:
raise InvalidBasesError("Cannot resolve one or more bases from %r" % (self.bases,))
# Turn fields into a dict for the body, add other bits
body = dict(self.fields)
View
7 django/db/models/base.py
@@ -151,9 +151,10 @@ def __new__(cls, name, bases, attrs):
new_class._base_manager = new_class._base_manager._copy_to_model(new_class)
# Bail out early if we have already created this class.
- m = new_class._meta.apps.get_registered_model(new_class._meta.app_label, name)
- if m is not None:
- return m
+ try:
+ return new_class._meta.apps.get_registered_model(new_class._meta.app_label, name)
+ except LookupError:
+ pass
# Add all attributes to the class.
for obj_name, obj in attrs.items():
View
9 django/db/models/fields/related.py
@@ -68,13 +68,14 @@ class MyModel(Model):
# string right away. If get_model returns None, it means that the related
# model isn't loaded yet, so we need to pend the relation until the class
# is prepared.
- model = cls._meta.apps.get_registered_model(app_label, model_name)
- if model:
- operation(field, model, cls)
- else:
+ try:
+ model = cls._meta.apps.get_registered_model(app_label, model_name)
+ except LookupError:
key = (app_label, model_name)
value = (cls, field, operation)
cls._meta.apps._pending_lookups.setdefault(key, []).append(value)
+ else:
+ operation(field, model, cls)
def do_pending_lookups(sender, **kwargs):
View
5 django/db/models/signals.py
@@ -39,8 +39,9 @@ def connect(self, receiver, sender=None, weak=True, dispatch_uid=None):
"Specified sender must either be a model or a "
"model name of the 'app_label.ModelName' form."
)
- sender = apps.get_registered_model(app_label, model_name)
- if sender is None:
+ try:
+ sender = apps.get_registered_model(app_label, model_name)
+ except LookupError:
ref = (app_label, model_name)
refs = self.unresolved_references.setdefault(ref, [])
refs.append((receiver, weak, dispatch_uid))
View
4 docs/ref/applications.txt
@@ -202,5 +202,5 @@ Application registry
.. method:: apps.get_model(app_label, model_name)
Returns the :class:`~django.db.models.Model` with the given ``app_label``
- and ``model_name``. Returns ``None`` if no such application or model
- exists. ``model_name`` is case-insensitive.
+ and ``model_name``. Raises :exc:`~exceptions.LookupError` if no such
+ application or model exists. ``model_name`` is case-insensitive.
View
12 docs/releases/1.7.txt
@@ -602,9 +602,15 @@ in addition to application modules, you should review code that accesses this
setting directly and use the app registry (:attr:`django.apps.apps`) instead.
The "app registry" that manages the list of installed applications doesn't
-have the same features as the old "app cache". However, even though the "app
-cache" was a private API, most of its methods were temporarily preserved and
-will go through a deprecation path.
+have the same features as the old "app cache". Even though the "app cache" was
+a private API, obsolete methods will be removed after a standard deprecation
+period. In addition, the following changes take effect immediately:
+
+* ``get_model`` raises :exc:`~exceptions.LookupError` instead of returning
+ ``None`` when no model is found.
+
+* The ``only_installed`` and ``seed_cache`` arguments of ``get_model`` no
+ longer exist.
While the new implementation is believed to be more robust, regressions cannot
be ruled out, especially during the import sequence. Circular imports that
View
4 tests/app_loading/tests.py
@@ -72,8 +72,8 @@ def setUp(self):
self.not_installed_module = models
def test_get_model_only_returns_installed_models(self):
- self.assertEqual(
- apps.get_model("not_installed", "NotInstalledModel"), None)
+ with self.assertRaises(LookupError):
+ apps.get_model("not_installed", "NotInstalledModel")
def test_get_models_only_returns_installed_models(self):
self.assertNotIn(
View
8 tests/apps/tests.py
@@ -148,9 +148,11 @@ def test_models_py(self):
Tests that the models in the models.py file were loaded correctly.
"""
self.assertEqual(apps.get_model("apps", "TotallyNormal"), TotallyNormal)
- self.assertEqual(apps.get_model("apps", "SoAlternative"), None)
+ with self.assertRaises(LookupError):
+ apps.get_model("apps", "SoAlternative")
- self.assertEqual(new_apps.get_model("apps", "TotallyNormal"), None)
+ with self.assertRaises(LookupError):
+ new_apps.get_model("apps", "TotallyNormal")
self.assertEqual(new_apps.get_model("apps", "SoAlternative"), SoAlternative)
def test_dynamic_load(self):
@@ -174,4 +176,6 @@ def test_dynamic_load(self):
old_models,
apps.get_models(apps.get_app_config("apps").models_module),
)
+ with self.assertRaises(LookupError):
+ apps.get_model("apps", "SouthPonies")
self.assertEqual(new_apps.get_model("apps", "SouthPonies"), temp_model)
Please sign in to comment.
Something went wrong with that request. Please try again.