summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Pellerin <jpellerin@gmail.com>2006-07-07 21:27:27 +0000
committerJason Pellerin <jpellerin@gmail.com>2006-07-07 21:27:27 +0000
commit774026e5c57e983828777adbcb51c94bafd06bc9 (patch)
tree1b5c948776e428bd3029b77b11496cc9134a9286
parent0d9dee02bbd9297ea9ad1187654e5b2db733e677 (diff)
[multi-db] Added _app_model_order to django.db.models.loading, a dict of
models keyed by app label in which the models for the app are listed in the order in which they were created. git-svn-id: http://code.djangoproject.com/svn/django/branches/multiple-db-support@3290 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/db/models/loading.py14
1 files changed, 12 insertions, 2 deletions
diff --git a/django/db/models/loading.py b/django/db/models/loading.py
index c7920fa4e0..2feca99240 100644
--- a/django/db/models/loading.py
+++ b/django/db/models/loading.py
@@ -12,6 +12,9 @@ _app_list = [] # Cache of installed apps.
_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.
+_app_model_order = {} # Dictionary of models against app label
+ # Each value is a list of model names, in the order in
+ # which the models were created
_app_errors = {} # Dictionary of errors that were experienced when loading the INSTALLED_APPS
# Key is the app_name of the model, value is the exception that was raised
# during model loading.
@@ -61,14 +64,19 @@ def get_app_errors():
get_apps() # Run get_apps() to populate the _app_list cache. Slightly hackish.
return _app_errors
-def get_models(app_mod=None):
+def get_models(app_mod=None, creation_order=False):
"""
Given a module containing models, returns a list of the models. Otherwise
returns a list of all installed models.
"""
app_list = get_apps() # Run get_apps() to populate the _app_list cache. Slightly hackish.
if app_mod:
- return _app_models.get(app_mod.__name__.split('.')[-2], {}).values()
+ app_label = app_mod.__name__.split('.')[-2]
+ app_models = _app_models.get(app_label, {})
+ if creation_order:
+ return [ app_models[name]
+ for name in _app_model_order.get(app_label, []) ]
+ return app_models.values()
else:
model_list = []
for app_mod in app_list:
@@ -99,6 +107,8 @@ def register_models(app_label, *models):
# in the _app_models dictionary
model_name = model._meta.object_name.lower()
model_dict = _app_models.setdefault(app_label, {})
+ model_list = _app_model_order.setdefault(app_label, [])
+ model_list.append(model_name)
if model_dict.has_key(model_name):
# The same model may be imported via different paths (e.g.
# appname.models and project.appname.models). We use the source