summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2013-12-28 14:41:11 +0100
committerAymeric Augustin <aymeric.augustin@m4x.org>2013-12-28 20:43:29 +0100
commit54790e669d73269bea6320c947ed8a46e98b8025 (patch)
treed1a543edc0f5ab904f66df3af7b883301562142d
parent81354b82bf647fd100e836dae2639e9c8d76c5eb (diff)
Simplified Apps.get_model and added AppConfig.get_model.
Documented them as public APIs.
-rw-r--r--django/apps/base.py17
-rw-r--r--django/apps/registry.py28
-rw-r--r--docs/ref/applications.txt15
3 files changed, 46 insertions, 14 deletions
diff --git a/django/apps/base.py b/django/apps/base.py
index e0d637c96e..31d75bf5ed 100644
--- a/django/apps/base.py
+++ b/django/apps/base.py
@@ -109,6 +109,23 @@ class AppConfig(object):
# Entry is a path to an app module.
return cls(entry, module)
+ def get_model(self, model_name):
+ """
+ Returns the model with the given case-insensitive model_name.
+
+ Raises LookupError if no model exists with this name.
+
+ This method assumes that apps.populate_models() has run.
+ """
+ if self.models is None:
+ raise LookupError(
+ "App '%s' doesn't have any models." % self.label)
+ try:
+ return self.models[model_name.lower()]
+ except KeyError:
+ raise LookupError(
+ "App '%s' doesn't have a '%s' model." % (self.label, model_name))
+
def import_models(self, all_models):
# Dictionary of models for this app, primarily maintained in the
# 'all_models' attribute of the Apps this AppConfig is attached to.
diff --git a/django/apps/registry.py b/django/apps/registry.py
index 6556fbf852..4f10ad8346 100644
--- a/django/apps/registry.py
+++ b/django/apps/registry.py
@@ -185,7 +185,7 @@ class Apps(object):
if app_config is None:
raise LookupError("No installed app with label '%s'." % app_label)
if only_with_models_module and app_config.models_module is None:
- raise LookupError("App with label '%s' doesn't have a models module." % app_label)
+ raise LookupError("App '%s' doesn't have a models module." % app_label)
return app_config
# This method is performance-critical at least for Django's test suite.
@@ -242,21 +242,20 @@ class Apps(object):
)
return model_list
- def get_model(self, app_label, model_name, only_installed=True):
+ def get_model(self, app_label, model_name):
"""
- Returns the model matching the given app_label and case-insensitive
- model_name.
+ Returns the model matching the given app_label and model_name.
- Returns None if no model is found.
+ model_name is case-insensitive.
+
+ Returns None if no application exists with this label, or no model
+ exists with this name in the application.
"""
- if not self.master:
- only_installed = False
self.populate_models()
- if only_installed:
- app_config = self.app_configs.get(app_label)
- if app_config is None:
- return None
- return self.all_models[app_label].get(model_name.lower())
+ 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
@@ -286,10 +285,11 @@ class Apps(object):
def get_registered_model(self, app_label, model_name):
"""
- Returns the model class if one is registered and None otherwise.
+ Similar to get_model(), but doesn't require that an app exists with
+ the given app_label.
It's safe to call this method at import time, even while the registry
- is being populated. It returns False for models that aren't loaded yet.
+ is being populated. It returns None for models that aren't loaded yet.
"""
return self.all_models[app_label].get(model_name.lower())
diff --git a/docs/ref/applications.txt b/docs/ref/applications.txt
index 15cc481f51..8f868f751e 100644
--- a/docs/ref/applications.txt
+++ b/docs/ref/applications.txt
@@ -153,6 +153,15 @@ Read-only attributes
It may be ``None`` if the application doesn't contain a ``models`` module.
+Methods
+-------
+
+.. method:: AppConfig.get_model(model_name)
+
+ Returns the :class:`~django.db.models.Model` with the given
+ ``model_name``. Raises :exc:`~exceptions.LookupError` if no such model
+ exists. ``model_name`` is case-insensitive.
+
Application registry
====================
@@ -189,3 +198,9 @@ Application registry
Unlike :meth:`~django.apps.apps.get_app_config`, this method can be called
safely at import time. If the registry is still being populated, it may
return ``False``, even though the app will become available later.
+
+.. 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.