diff --git a/django/core/apps.py b/django/core/apps.py new file mode 100644 index 0000000000..5c2844e036 --- /dev/null +++ b/django/core/apps.py @@ -0,0 +1,9 @@ +class App(object): + def __init__(self, name, models): + # fully qualified name (e.g. 'django.contrib.auth') + self.name = name + self.label = name.split('.')[-1] + self.models = models + + def __repr__(self): + return '' % self.name diff --git a/django/db/models/loading.py b/django/db/models/loading.py index 620cebc25c..d01e33b411 100644 --- a/django/db/models/loading.py +++ b/django/db/models/loading.py @@ -5,6 +5,7 @@ from django.core.exceptions import ImproperlyConfigured from django.utils.datastructures import SortedDict from django.utils.importlib import import_module from django.utils.module_loading import module_has_submodule +from django.core.apps import App import imp import sys @@ -22,6 +23,9 @@ class AppCache(object): # Use the Borg pattern to share state between all instances. Details at # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531. __shared_state = dict( + # List of App instances + app_instances = [], + # Keys of app_store are the model modules for each application. app_store = SortedDict(), @@ -99,6 +103,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_instances.append(App(app_name, models)) return models def app_cache_ready(self): diff --git a/tests/appcachetests.py b/tests/appcachetests.py index b285d16094..5ac09a9178 100644 --- a/tests/appcachetests.py +++ b/tests/appcachetests.py @@ -195,6 +195,8 @@ class RegisterModelsTests(AppCacheTestCase): cache.register_models('foo', *(FlatPage, Site,)) self.assertFalse(cache.app_cache_ready()) rv = cache.get_models() + # we have 4 models since the above import will trigger the + # ModelBase.__new__, which will call the register_models function self.assertEqual(len(rv), 4) self.assertEqual(rv[0], Site) self.assertEqual(rv[1], FlatPage)