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):
# 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)

View File

@ -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):

View File

@ -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)):

View File

@ -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(

View File

@ -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 = []

View File

@ -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

View File

@ -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):

View File

@ -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:

View File

@ -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