@@ -46,6 +46,9 @@ class AppCache(object):
46
46
47
47
def __init__ (self ):
48
48
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 ))
49
52
50
53
def _populate (self ):
51
54
"""
@@ -103,9 +106,17 @@ def load_app(self, app_name, can_postpone=False):
103
106
self .nesting_level -= 1
104
107
if models not in self .app_store :
105
108
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
107
112
return models
108
113
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
+
109
120
def app_cache_ready (self ):
110
121
"""
111
122
Returns true if the model cache is fully populated.
@@ -149,7 +160,9 @@ def get_app(self, app_label, emptyOK=False):
149
160
def get_app_errors (self ):
150
161
"Returns the map of known problems with the INSTALLED_APPS."
151
162
self ._populate ()
152
- return self .app_errors
163
+ for app in app_instances :
164
+ self .app_errors .update (app .errors )
165
+ return errors
153
166
154
167
def get_models (self , app_mod = None , include_auto_created = False , include_deferred = False ):
155
168
"""
@@ -173,11 +186,13 @@ def get_models(self, app_mod=None, include_auto_created=False, include_deferred=
173
186
if app_mod :
174
187
app_list = [self .app_models .get (app_mod .__name__ .split ('.' )[- 2 ], SortedDict ())]
175
188
else :
176
- app_list = self .app_models .itervalues ()
189
+ #app_list = self.app_models.itervalues()
190
+ app_list = self .app_instances
177
191
model_list = []
178
192
for app in app_list :
193
+ models = app .models
179
194
model_list .extend (
180
- model for model in app .values ()
195
+ model for model in models # app.values()
181
196
if ((not model ._deferred or include_deferred )
182
197
and (not model ._meta .auto_created or include_auto_created ))
183
198
)
@@ -193,12 +208,22 @@ def get_model(self, app_label, model_name, seed_cache=True):
193
208
"""
194
209
if seed_cache :
195
210
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
197
216
198
217
def register_models (self , app_label , * models ):
199
218
"""
200
219
Register a set of models as belonging to an app.
201
220
"""
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 )
202
227
for model in models :
203
228
# Store as 'name: model' pair in a dictionary
204
229
# in the app_models dictionary
@@ -216,6 +241,7 @@ def register_models(self, app_label, *models):
216
241
if os .path .splitext (fname1 )[0 ] == os .path .splitext (fname2 )[0 ]:
217
242
continue
218
243
model_dict [model_name ] = model
244
+ app .models .append (model )
219
245
self ._get_models_cache .clear ()
220
246
221
247
cache = AppCache ()
0 commit comments