8000 [soc2010/app-loading] save models in app; adjust get_model/get_models · ddriddle/django@06f15e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 06f15e4

Browse files
committed
[soc2010/app-loading] save models in app; adjust get 8000 _model/get_models
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/app-loading@13403 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent ab037f2 commit 06f15e4

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

django/core/apps.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
class App(object):
2-
def __init__(self, name, models):
3-
# fully qualified name (e.g. 'django.contrib.auth')
4-
self.name = name
5-
self.label = name.split('.')[-1]
6-
self.models = models
2+
def __init__(self, label):
3+
if '.' in label:
4+
label = label.split('.')[-1]
5+
self.label = label
6+
self.errors = {}
7+
self.models = []
8+
self.models_module = None
79

810
def __repr__(self):
9-
return '<App: %s>' % self.name
11+
return '<App: %s>' % self.label

django/db/models/loading.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class AppCache(object):
4646

4747
def __init__(self):
4848
self.__dict__ = self.__shared_state
49+
# Create App instances for the apps in INSTALLED_APPS
50+
for app_name in settings.INSTALLED_APPS:
51+
self.app_instances.append(App(app_name))
4952

5053
def _populate(self):
5154
"""
@@ -103,9 +106,17 @@ def load_app(self, app_name, can_postpone=False):
103106
self.nesting_level -= 1
104107
if models not in self.app_store:
105108
self.app_store[models] = len(self.app_store)
106-
self.app_instances.append(App(app_name, models))
109+
app = self.find_app(app_name.split('.')[-1])
110+
if app:
111+
app.models_module = models
107112
return models
108113

114+
def find_app(self, app_label):
115+
"Returns the App instance that matches app_label"
116+
for app in self.app_instances:
117+
if app.label == app_label:
118+
return app
119+
109120
def app_cache_ready(self):
110121
"""
111122
Returns true if the model cache is fully populated.
@@ -149,7 +160,9 @@ def get_app(self, app_label, emptyOK=False):
149160
def get_app_errors(self):
150161
"Returns the map of known problems with the INSTALLED_APPS."
151162
self._populate()
152-
return self.app_errors
163+
for app in app_instances:
164+
self.app_errors.update(app.errors)
165+
return errors
153166

154167
def get_models(self, app_mod=None, include_auto_created=False, include_deferred=False):
155168
"""
@@ -173,11 +186,13 @@ def get_models(self, app_mod=None, include_auto_created=False, include_deferred=
173186
if app_mod:
174187
app_list = [self.app_models.get(app_mod.__name__.split('.')[-2], SortedDict())]
175188
else:
176-
app_list = self.app_models.itervalues()
189+
#app_list = self.app_models.itervalues()
190+
app_list = self.app_instances
177191
model_list = []
178192
for app in app_list:
193+
models = app.models
179194
model_list.extend(
180-
model for model in app.values()
195+
model for model in models#app.values()
181196
if ((not model._deferred or include_deferred)
182197
and (not model._meta.auto_created or include_auto_created))
183198
)
@@ -193,12 +208,22 @@ def get_model(self, app_label, model_name, seed_cache=True):
193208
"""
194209
if seed_cache:
195210
self._populate()
196-
return self.app_models.get(app_label, SortedDict()).get(model_name.lower())
211+
app = self.find_app(app_label)
212+
if app:
213+
for model in app.models:
214+
if model_name.lower() == model._meta.object_name.lower():
215+
return model
197216

198217
def register_models(self, app_label, *models):
199218
"""
200219
Register a set of models as belonging to an app.
201220
"""
221+
# Create a new App instance if an app in INSTALLED_APPS
222+
# imports another package that has models
223+
app = self.find_app(app_label)
224+
if not app:
225+
app = App(app_label)
226+
self.app_instances.append(app)
202227
for model in models:
203228
# Store as 'name: model' pair in a dictionary
204229
# in the app_models dictionary
@@ -216,6 +241,7 @@ def register_models(self, app_label, *models):
216241
if os.path.splitext(fname1)[0] == os.path.splitext(fname2)[0]:
217242
continue
218243
model_dict[model_name] = model
244+
app.models.append(model)
219245
self._get_models_cache.clear()
220246

221247
cache = AppCache()

0 commit comments

Comments
 (0)
0