diff --git a/django/contrib/admindocs/views.py b/django/contrib/admindocs/views.py index 7afb11ef34..75f2c13060 100644 --- a/django/contrib/admindocs/views.py +++ b/django/contrib/admindocs/views.py @@ -185,11 +185,11 @@ class ModelDetailView(BaseAdminDocsView): def get_context_data(self, **kwargs): # Get the model class. try: - apps.get_app_config(self.kwargs['app_label']) + app_config = 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']) + model = app_config.get_model(self.kwargs['model_name']) except LookupError: raise Http404(_("Model %(model_name)r not found in app %(app_label)r") % self.kwargs) diff --git a/django/contrib/auth/__init__.py b/django/contrib/auth/__init__.py index 77b2482e31..9fd46814b2 100644 --- a/django/contrib/auth/__init__.py +++ b/django/contrib/auth/__init__.py @@ -1,6 +1,7 @@ import inspect import re +from django.apps import apps as django_apps from django.conf import settings from django.core.exceptions import ImproperlyConfigured, PermissionDenied from django.utils.module_loading import import_by_path @@ -123,17 +124,12 @@ def get_user_model(): """ Returns the User model that is active in this project. """ - from django.apps import apps - try: - app_label, model_name = settings.AUTH_USER_MODEL.split('.') + return django_apps.get_model(settings.AUTH_USER_MODEL) 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) except LookupError: raise ImproperlyConfigured("AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL) - return user_model def get_user(request): diff --git a/django/contrib/auth/checks.py b/django/contrib/auth/checks.py index f16da4dd47..ec55c29665 100644 --- a/django/contrib/auth/checks.py +++ b/django/contrib/auth/checks.py @@ -2,16 +2,14 @@ from __future__ import unicode_literals from django.apps import apps +from django.conf import settings from django.core import checks def check_user_model(**kwargs): - from django.conf import settings - errors = [] - app_name, model_name = settings.AUTH_USER_MODEL.split('.') - cls = apps.get_model(app_name, model_name) + cls = apps.get_model(settings.AUTH_USER_MODEL) # Check that REQUIRED_FIELDS is a list if not isinstance(cls.REQUIRED_FIELDS, (list, tuple)): diff --git a/django/contrib/comments/views/comments.py b/django/contrib/comments/views/comments.py index 165f2b6574..c124634088 100644 --- a/django/contrib/comments/views/comments.py +++ b/django/contrib/comments/views/comments.py @@ -49,7 +49,7 @@ def post_comment(request, next=None, using=None): if ctype is None or object_pk is None: return CommentPostBadRequest("Missing content_type or object_pk field.") try: - model = apps.get_model(*ctype.split(".", 1)) + model = apps.get_model(ctype) target = model._default_manager.using(using).get(pk=object_pk) except TypeError: return CommentPostBadRequest( diff --git a/django/core/management/commands/dumpdata.py b/django/core/management/commands/dumpdata.py index 1242bd4e57..eb5d9ffd50 100644 --- a/django/core/management/commands/dumpdata.py +++ b/django/core/management/commands/dumpdata.py @@ -65,9 +65,8 @@ class Command(BaseCommand): excluded_models = set() for exclude in excludes: if '.' in exclude: - app_label, model_name = exclude.split('.', 1) try: - model = apps.get_model(app_label, model_name) + model = apps.get_model(exclude) except LookupError: raise CommandError('Unknown model in excludes: %s' % exclude) excluded_models.add(model) @@ -98,7 +97,7 @@ class Command(BaseCommand): if app_config.models_module is None or app_config in excluded_apps: continue try: - model = apps.get_model(app_label, model_label) + model = app_config.get_model(model_label) except LookupError: raise CommandError("Unknown model: %s.%s" % (app_label, model_label)) @@ -177,7 +176,7 @@ def sort_dependencies(app_list): if hasattr(model, 'natural_key'): deps = getattr(model.natural_key, 'dependencies', []) if deps: - deps = [apps.get_model(*d.split('.')) for d in deps] + deps = [apps.get_model(dep) for dep in deps] else: deps = [] diff --git a/django/core/serializers/python.py b/django/core/serializers/python.py index 3d14a1b509..249dc5a054 100644 --- a/django/core/serializers/python.py +++ b/django/core/serializers/python.py @@ -153,7 +153,6 @@ def _get_model(model_identifier): Helper to look up a model from an "app_label.model_name" string. """ try: - Model = apps.get_model(*model_identifier.split(".")) + return apps.get_model(model_identifier) except (LookupError, TypeError): raise base.DeserializationError("Invalid model identifier: '%s'" % model_identifier) - return Model diff --git a/django/core/serializers/xml_serializer.py b/django/core/serializers/xml_serializer.py index 06dd134754..a23e74454f 100644 --- a/django/core/serializers/xml_serializer.py +++ b/django/core/serializers/xml_serializer.py @@ -277,12 +277,11 @@ class Deserializer(base.Deserializer): "<%s> node is missing the required '%s' attribute" % (node.nodeName, attr)) try: - Model = apps.get_model(*model_identifier.split(".")) + return apps.get_model(model_identifier) except (LookupError, TypeError): raise base.DeserializationError( "<%s> node has invalid model identifier: '%s'" % (node.nodeName, model_identifier)) - return Model def getInnerText(node): diff --git a/django/db/migrations/state.py b/django/db/migrations/state.py index ab4c4be2f7..f802b6cc0b 100644 --- a/django/db/migrations/state.py +++ b/django/db/migrations/state.py @@ -194,7 +194,7 @@ class ModelState(object): # Then, work out our bases try: bases = tuple( - (apps.get_model(*base.split(".", 1)) if isinstance(base, six.string_types) else base) + (apps.get_model(base) if isinstance(base, six.string_types) else base) for base in self.bases ) except LookupError: diff --git a/django/db/models/base.py b/django/db/models/base.py index 9e835d0e16..65e1f5d9e4 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -1054,7 +1054,7 @@ class Model(six.with_metaclass(ModelBase)): errors = [] if cls._meta.swapped: try: - app_label, model_name = cls._meta.swapped.split('.') + apps.get_model(cls._meta.swapped) except ValueError: errors.append( checks.Error( @@ -1064,24 +1064,22 @@ class Model(six.with_metaclass(ModelBase)): id='E002', ) ) - else: - try: - apps.get_model(app_label, model_name) - except LookupError: - errors.append( - checks.Error( - ('The model has been swapped out for %s.%s ' - 'which has not been installed or is abstract.') % ( - app_label, model_name - ), - hint=('Ensure that you did not misspell the model ' - 'name and the app name as well as the model ' - 'is not abstract. Does your INSTALLED_APPS ' - 'setting contain the "%s" app?') % app_label, - obj=cls, - id='E003', - ) + except LookupError: + app_label, model_name = cls._meta.swapped.split('.') + errors.append( + checks.Error( + ('The model has been swapped out for %s.%s ' + 'which has not been installed or is abstract.') % ( + app_label, model_name + ), + hint=('Ensure that you did not misspell the model ' + 'name and the app name as well as the model ' + 'is not abstract. Does your INSTALLED_APPS ' + 'setting contain the "%s" app?') % app_label, + obj=cls, + id='E003', ) + ) return errors @classmethod