summaryrefslogtreecommitdiff
path: root/django/db/models/loading.py
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2006-06-23 04:37:00 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2006-06-23 04:37:00 +0000
commit0d4b5b9b4a203b98676d800544bbd877a5208bd6 (patch)
tree8a4829dad29527a3d5f56355f4511370e8232dd5 /django/db/models/loading.py
parentbc2d8cdbc6d923dedc153949fb3a97e0cdd0c43d (diff)
Fixed #1662 -- Added resolver for string-form model references for models that have already been loaded, with tests to validate both forward and backward referenced model names. Light refactoring of model loading to make regression tests behave more like normal model loading. Also clarifies the text of some validation errors.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@3195 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/db/models/loading.py')
-rw-r--r--django/db/models/loading.py35
1 files changed, 22 insertions, 13 deletions
diff --git a/django/db/models/loading.py b/django/db/models/loading.py
index 7f5c97924b..4eafc22fb4 100644
--- a/django/db/models/loading.py
+++ b/django/db/models/loading.py
@@ -5,36 +5,45 @@ from django.core.exceptions import ImproperlyConfigured
__all__ = ('get_apps', 'get_app', 'get_models', 'get_model', 'register_models')
-_app_list = None # Cache of installed apps.
+_app_list = [] # Cache of installed apps.
+ # Entry is not placed in app_list cache until entire app is loaded.
_app_models = {} # Dictionary of models against app label
# Each value is a dictionary of model name: model class
+ # Applabel and Model entry exists in cache when individual model is loaded.
+_loaded = False # Has the contents of settings.INSTALLED_APPS been loaded?
+ # i.e., has get_apps() been called?
def get_apps():
"Returns a list of all installed modules that contain models."
global _app_list
- if _app_list is not None:
- return _app_list
- _app_list = []
- for app_name in settings.INSTALLED_APPS:
- try:
- mod = __import__(app_name, '', '', ['models'])
- except ImportError:
- pass # Assume this app doesn't have a models.py in it.
- # GOTCHA: It may have a models.py that raises ImportError.
- else:
+ global _loaded
+ if not _loaded:
+ _loaded = True
+ for app_name in settings.INSTALLED_APPS:
try:
- _app_list.append(mod.models)
+ load_app(app_name)
+ except ImportError:
+ pass # Assume this app doesn't have a models.py in it.
+ # GOTCHA: It may have a models.py that raises ImportError.
except AttributeError:
pass # This app doesn't have a models.py in it.
return _app_list
def get_app(app_label):
"Returns the module containing the models for the given app_label."
+ get_apps() # Run get_apps() to populate the _app_list cache. Slightly hackish.
for app_name in settings.INSTALLED_APPS:
if app_label == app_name.split('.')[-1]:
- return __import__(app_name, '', '', ['models']).models
+ return load_app(app_name)
raise ImproperlyConfigured, "App with label %s could not be found" % app_label
+def load_app(app_name):
+ "Loads the app with the provided fully qualified name, and returns the model module."
+ mod = __import__(app_name, '', '', ['models'])
+ if mod.models not in _app_list:
+ _app_list.append(mod.models)
+ return mod.models
+
def get_models(app_mod=None):
"""
Given a module containing models, returns a list of the models. Otherwise