mirror of https://github.com/django/django.git
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.
This commit is contained in:
parent
54790e669d
commit
ba7206cd81
|
@ -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):
|
||||
"""
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -157,7 +157,10 @@ class ContentType(models.Model):
|
|||
|
||||
def model_class(self):
|
||||
"Returns the Python model class for this type of content."
|
||||
try:
|
||||
return apps.get_model(self.app_label, self.model)
|
||||
except LookupError:
|
||||
return None
|
||||
|
||||
def get_object_for_this_type(self, **kwargs):
|
||||
"""
|
||||
|
|
|
@ -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 = []
|
||||
try:
|
||||
klass = apps.get_model(label, model)
|
||||
if not klass:
|
||||
except LookupError:
|
||||
raise Http404('You must supply a valid app label and module name. Got "%s.%s"' % (label, model))
|
||||
|
||||
if field_name:
|
||||
|
|
|
@ -66,8 +66,9 @@ class Command(BaseCommand):
|
|||
for exclude in excludes:
|
||||
if '.' in exclude:
|
||||
app_label, model_name = exclude.split('.', 1)
|
||||
try:
|
||||
model = apps.get_model(app_label, model_name)
|
||||
if not model:
|
||||
except LookupError:
|
||||
raise CommandError('Unknown model in excludes: %s' % exclude)
|
||||
excluded_models.add(model)
|
||||
else:
|
||||
|
@ -96,8 +97,9 @@ class Command(BaseCommand):
|
|||
raise CommandError("Unknown application: %s" % app_label)
|
||||
if app_config.models_module is None or app_config in excluded_apps:
|
||||
continue
|
||||
try:
|
||||
model = apps.get_model(app_label, model_label)
|
||||
if model is None:
|
||||
except LookupError:
|
||||
raise CommandError("Unknown model: %s.%s" % (app_label, model_label))
|
||||
|
||||
app_list_value = app_list.setdefault(app_config, [])
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -278,9 +278,7 @@ class Deserializer(base.Deserializer):
|
|||
% (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))
|
||||
|
|
|
@ -192,11 +192,12 @@ class ModelState(object):
|
|||
meta_contents["unique_together"] = list(meta_contents["unique_together"])
|
||||
meta = type("Meta", tuple(), meta_contents)
|
||||
# Then, work out our bases
|
||||
try:
|
||||
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:
|
||||
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)
|
||||
|
|
|
@ -151,9 +151,10 @@ class ModelBase(type):
|
|||
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():
|
||||
|
|
|
@ -68,13 +68,14 @@ def add_lazy_relation(cls, field, relation, operation):
|
|||
# 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.
|
||||
try:
|
||||
model = cls._meta.apps.get_registered_model(app_label, model_name)
|
||||
if model:
|
||||
operation(field, model, cls)
|
||||
else:
|
||||
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):
|
||||
|
|
|
@ -39,8 +39,9 @@ class ModelSignal(Signal):
|
|||
"Specified sender must either be a model or a "
|
||||
"model name of the 'app_label.ModelName' form."
|
||||
)
|
||||
try:
|
||||
sender = apps.get_registered_model(app_label, model_name)
|
||||
if sender is None:
|
||||
except LookupError:
|
||||
ref = (app_label, model_name)
|
||||
refs = self.unresolved_references.setdefault(ref, [])
|
||||
refs.append((receiver, weak, dispatch_uid))
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -72,8 +72,8 @@ class GetModelsTest(TestCase):
|
|||
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(
|
||||
|
|
|
@ -148,9 +148,11 @@ class AppsTests(TestCase):
|
|||
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 @@ class AppsTests(TestCase):
|
|||
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)
|
||||
|
|
Loading…
Reference in New Issue