|
@@ -25,7 +25,11 @@ class AppCache(object):
|
|
|
# Keys of app_store are the model modules for each application.
|
|
|
app_store = SortedDict(),
|
|
|
|
|
|
+ # Mapping of installed app_labels to model modules for that app.
|
|
|
+ app_labels = {},
|
|
|
+
|
|
|
# Mapping of app_labels to a dictionary of model names to model code.
|
|
|
+ # May contain apps that are not installed.
|
|
|
app_models = SortedDict(),
|
|
|
|
|
|
# Mapping of app_labels to errors raised when trying to import the app.
|
|
@@ -66,6 +70,13 @@ class AppCache(object):
|
|
|
finally:
|
|
|
self.write_lock.release()
|
|
|
|
|
|
+ def _label_for(self, app_mod):
|
|
|
+ """
|
|
|
+ Return app_label for given models module.
|
|
|
+
|
|
|
+ """
|
|
|
+ return app_mod.__name__.split('.')[-2]
|
|
|
+
|
|
|
def load_app(self, app_name, can_postpone=False):
|
|
|
"""
|
|
|
Loads the app with the provided fully qualified name, and returns the
|
|
@@ -99,6 +110,7 @@ class AppCache(object):
|
|
|
self.nesting_level -= 1
|
|
|
if models not in self.app_store:
|
|
|
self.app_store[models] = len(self.app_store)
|
|
|
+ self.app_labels[self._label_for(models)] = models
|
|
|
return models
|
|
|
|
|
|
def app_cache_ready(self):
|
|
@@ -146,7 +158,8 @@ class AppCache(object):
|
|
|
self._populate()
|
|
|
return self.app_errors
|
|
|
|
|
|
- def get_models(self, app_mod=None, include_auto_created=False, include_deferred=False):
|
|
|
+ def get_models(self, app_mod=None,
|
|
|
+ include_auto_created=False, include_deferred=False):
|
|
|
"""
|
|
|
Given a module containing models, returns a list of the models.
|
|
|
Otherwise returns a list of all installed models.
|
|
@@ -166,20 +179,26 @@ class AppCache(object):
|
|
|
pass
|
|
|
self._populate()
|
|
|
if app_mod:
|
|
|
- app_list = [self.app_models.get(app_mod.__name__.split('.')[-2], SortedDict())]
|
|
|
+ if app_mod in self.app_store:
|
|
|
+ app_list = [self.app_models.get(self._label_for(app_mod),
|
|
|
+ SortedDict())]
|
|
|
+ else:
|
|
|
+ app_list = []
|
|
|
else:
|
|
|
- app_list = self.app_models.itervalues()
|
|
|
+ app_list = [self.app_models.get(app_label, SortedDict())
|
|
|
+ for app_label in self.app_labels.iterkeys()]
|
|
|
model_list = []
|
|
|
for app in app_list:
|
|
|
model_list.extend(
|
|
|
model for model in app.values()
|
|
|
- if ((not model._deferred or include_deferred)
|
|
|
- and (not model._meta.auto_created or include_auto_created))
|
|
|
+ if ((not model._deferred or include_deferred) and
|
|
|
+ (not model._meta.auto_created or include_auto_created))
|
|
|
)
|
|
|
self._get_models_cache[cache_key] = model_list
|
|
|
return model_list
|
|
|
|
|
|
- def get_model(self, app_label, model_name, seed_cache=True):
|
|
|
+ def get_model(self, app_label, model_name,
|
|
|
+ seed_cache=True, only_installed=True):
|
|
|
"""
|
|
|
Returns the model matching the given app_label and case-insensitive
|
|
|
model_name.
|
|
@@ -188,6 +207,8 @@ class AppCache(object):
|
|
|
"""
|
|
|
if seed_cache:
|
|
|
self._populate()
|
|
|
+ if only_installed and app_label not in self.app_labels:
|
|
|
+ return None
|
|
|
return self.app_models.get(app_label, SortedDict()).get(model_name.lower())
|
|
|
|
|
|
def register_models(self, app_label, *models):
|