Took advantage of the new get_model API. Refs #21702.

This commit is contained in:
Aymeric Augustin 2014-01-26 12:57:08 +01:00
parent 3c47786cb9
commit f901b4d6c8
9 changed files with 29 additions and 40 deletions

View File

@ -185,11 +185,11 @@ class ModelDetailView(BaseAdminDocsView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
# Get the model class. # Get the model class.
try: try:
apps.get_app_config(self.kwargs['app_label']) app_config = apps.get_app_config(self.kwargs['app_label'])
except LookupError: except LookupError:
raise Http404(_("App %(app_label)r not found") % self.kwargs) raise Http404(_("App %(app_label)r not found") % self.kwargs)
try: try:
model = apps.get_model(self.kwargs['app_label'], self.kwargs['model_name']) model = app_config.get_model(self.kwargs['model_name'])
except LookupError: except LookupError:
raise Http404(_("Model %(model_name)r not found in app %(app_label)r") % self.kwargs) raise Http404(_("Model %(model_name)r not found in app %(app_label)r") % self.kwargs)

View File

@ -1,6 +1,7 @@
import inspect import inspect
import re import re
from django.apps import apps as django_apps
from django.conf import settings from django.conf import settings
from django.core.exceptions import ImproperlyConfigured, PermissionDenied from django.core.exceptions import ImproperlyConfigured, PermissionDenied
from django.utils.module_loading import import_by_path 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. Returns the User model that is active in this project.
""" """
from django.apps import apps
try: try:
app_label, model_name = settings.AUTH_USER_MODEL.split('.') return django_apps.get_model(settings.AUTH_USER_MODEL)
except ValueError: except ValueError:
raise ImproperlyConfigured("AUTH_USER_MODEL must be of the form 'app_label.model_name'") 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: except LookupError:
raise ImproperlyConfigured("AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL) 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): def get_user(request):

View File

@ -2,16 +2,14 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.apps import apps from django.apps import apps
from django.conf import settings
from django.core import checks from django.core import checks
def check_user_model(**kwargs): def check_user_model(**kwargs):
from django.conf import settings
errors = [] 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 # Check that REQUIRED_FIELDS is a list
if not isinstance(cls.REQUIRED_FIELDS, (list, tuple)): if not isinstance(cls.REQUIRED_FIELDS, (list, tuple)):

View File

@ -49,7 +49,7 @@ def post_comment(request, next=None, using=None):
if ctype is None or object_pk is None: if ctype is None or object_pk is None:
return CommentPostBadRequest("Missing content_type or object_pk field.") return CommentPostBadRequest("Missing content_type or object_pk field.")
try: try:
model = apps.get_model(*ctype.split(".", 1)) model = apps.get_model(ctype)
target = model._default_manager.using(using).get(pk=object_pk) target = model._default_manager.using(using).get(pk=object_pk)
except TypeError: except TypeError:
return CommentPostBadRequest( return CommentPostBadRequest(

View File

@ -65,9 +65,8 @@ class Command(BaseCommand):
excluded_models = set() excluded_models = set()
for exclude in excludes: for exclude in excludes:
if '.' in exclude: if '.' in exclude:
app_label, model_name = exclude.split('.', 1)
try: try:
model = apps.get_model(app_label, model_name) model = apps.get_model(exclude)
except LookupError: except LookupError:
raise CommandError('Unknown model in excludes: %s' % exclude) raise CommandError('Unknown model in excludes: %s' % exclude)
excluded_models.add(model) excluded_models.add(model)
@ -98,7 +97,7 @@ class Command(BaseCommand):
if app_config.models_module is None or app_config in excluded_apps: if app_config.models_module is None or app_config in excluded_apps:
continue continue
try: try:
model = apps.get_model(app_label, model_label) model = app_config.get_model(model_label)
except LookupError: except LookupError:
raise CommandError("Unknown model: %s.%s" % (app_label, model_label)) raise CommandError("Unknown model: %s.%s" % (app_label, model_label))
@ -177,7 +176,7 @@ def sort_dependencies(app_list):
if hasattr(model, 'natural_key'): if hasattr(model, 'natural_key'):
deps = getattr(model.natural_key, 'dependencies', []) deps = getattr(model.natural_key, 'dependencies', [])
if deps: if deps:
deps = [apps.get_model(*d.split('.')) for d in deps] deps = [apps.get_model(dep) for dep in deps]
else: else:
deps = [] deps = []

View File

@ -153,7 +153,6 @@ def _get_model(model_identifier):
Helper to look up a model from an "app_label.model_name" string. Helper to look up a model from an "app_label.model_name" string.
""" """
try: try:
Model = apps.get_model(*model_identifier.split(".")) return apps.get_model(model_identifier)
except (LookupError, TypeError): except (LookupError, TypeError):
raise base.DeserializationError("Invalid model identifier: '%s'" % model_identifier) raise base.DeserializationError("Invalid model identifier: '%s'" % model_identifier)
return Model

View File

@ -277,12 +277,11 @@ class Deserializer(base.Deserializer):
"<%s> node is missing the required '%s' attribute" "<%s> node is missing the required '%s' attribute"
% (node.nodeName, attr)) % (node.nodeName, attr))
try: try:
Model = apps.get_model(*model_identifier.split(".")) return apps.get_model(model_identifier)
except (LookupError, TypeError): except (LookupError, TypeError):
raise base.DeserializationError( raise base.DeserializationError(
"<%s> node has invalid model identifier: '%s'" "<%s> node has invalid model identifier: '%s'"
% (node.nodeName, model_identifier)) % (node.nodeName, model_identifier))
return Model
def getInnerText(node): def getInnerText(node):

View File

@ -194,7 +194,7 @@ class ModelState(object):
# Then, work out our bases # Then, work out our bases
try: try:
bases = tuple( 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 for base in self.bases
) )
except LookupError: except LookupError:

View File

@ -1054,7 +1054,7 @@ class Model(six.with_metaclass(ModelBase)):
errors = [] errors = []
if cls._meta.swapped: if cls._meta.swapped:
try: try:
app_label, model_name = cls._meta.swapped.split('.') apps.get_model(cls._meta.swapped)
except ValueError: except ValueError:
errors.append( errors.append(
checks.Error( checks.Error(
@ -1064,10 +1064,8 @@ class Model(six.with_metaclass(ModelBase)):
id='E002', id='E002',
) )
) )
else:
try:
apps.get_model(app_label, model_name)
except LookupError: except LookupError:
app_label, model_name = cls._meta.swapped.split('.')
errors.append( errors.append(
checks.Error( checks.Error(
('The model has been swapped out for %s.%s ' ('The model has been swapped out for %s.%s '