Commit ba7206cd authored by Aymeric Augustin's avatar Aymeric Augustin
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.
parent 54790e66
Loading
Loading
Loading
Loading
+9 −8
Original line number Diff line number Diff line
@@ -248,14 +248,11 @@ class Apps(object):

        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

    def register_model(self, app_label, model):
        # Since this method is called when models are imported, it cannot
@@ -289,9 +286,13 @@ class Apps(object):
        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):
        """
+3 −2
Original line number Diff line number Diff line
@@ -188,8 +188,9 @@ class ModelDetailView(BaseAdminDocsView):
            apps.get_app_config(self.kwargs['app_label'])
        except LookupError:
            raise Http404(_("App %(app_label)r not found") % self.kwargs)
        try:
            model = apps.get_model(self.kwargs['app_label'], self.kwargs['model_name'])
        if model is None:
        except LookupError:
            raise Http404(_("Model %(model_name)r not found in app %(app_label)r") % self.kwargs)

        opts = model._meta
+3 −2
Original line number Diff line number Diff line
@@ -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'")
    try:
        user_model = apps.get_model(app_label, model_name)
    if user_model is None:
    except LookupError:
        raise ImproperlyConfigured("AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL)
    return user_model

+6 −2
Original line number Diff line number Diff line
@@ -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()
+1 −1
Original line number Diff line number Diff line
@@ -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))
Loading