@@ -43,7 +43,7 @@ def __init__(self, installed_apps=()):
43
43
self .stored_app_configs = []
44
44
45
45
# Whether the registry is populated.
46
- self .ready = False
46
+ self .apps_ready = self . models_ready = self . ready = False
47
47
48
48
# Lock for thread-safe population.
49
49
self ._lock = threading .Lock ()
@@ -100,29 +100,41 @@ def populate(self, installed_apps=None):
100
100
"Application names aren't unique, "
101
101
"duplicates: %s" % ", " .join (duplicates ))
102
102
103
+ self .apps_ready = True
104
+
103
105
# Load models.
104
106
for app_config in self .app_configs .values ():
105
107
all_models = self .all_models [app_config .label ]
106
108
app_config .import_models (all_models )
107
109
108
110
self .clear_cache ()
109
- self .ready = True
111
+
112
+ self .models_ready = True
110
113
111
114
for app_config in self .get_app_configs ():
112
115
app_config .ready ()
113
116
114
- def check_ready (self ):
117
+ self .ready = True
118
+
119
+ def check_apps_ready (self ):
115
120
"""
116
- Raises an exception if the registry isn 't ready .
121
+ Raises an exception if all apps haven 't been imported yet .
117
122
"""
118
- if not self .ready :
119
- raise AppRegistryNotReady ()
123
+ if not self .apps_ready :
124
+ raise AppRegistryNotReady ("Apps aren't loaded yet." )
125
+
126
+ def check_models_ready (self ):
127
+ """
128
+ Raises an exception if all models haven't been imported yet.
129
+ """
130
+ if not self .models_ready :
131
+ raise AppRegistryNotReady ("Models aren't loaded yet." )
120
132
121
133
def get_app_configs (self ):
122
134
"""
123
135
Imports applications and returns an iterable of app configs.
124
136
"""
125
- self .check_ready ()
137
+ self .check_apps_ready ()
126
138
return self .app_configs .values ()
127
139
128
140
def get_app_config (self , app_label ):
@@ -131,7 +143,7 @@ def get_app_config(self, app_label):
131
143
132
144
Raises LookupError if no application exists with this label.
133
145
"""
134
- self .check_ready ()
146
+ self .check_apps_ready ()
135
147
try :
136
148
return self .app_configs [app_label ]
137
149
except KeyError :
@@ -153,7 +165,7 @@ def get_models(self, app_mod=None, include_auto_created=False,
153
165
154
166
Set the corresponding keyword argument to True to include such models.
155
167
"""
156
- self .check_ready ()
168
+ self .check_models_ready ()
157
169
if app_mod :
158
170
warnings .warn (
159
171
"The app_mod argument of get_models is deprecated." ,
@@ -184,7 +196,7 @@ def get_model(self, app_label, model_name=None):
184
196
model exists with this name in the application. Raises ValueError if
185
197
called with a single argument that doesn't contain exactly one dot.
186
198
"""
187
- self .check_ready ()
199
+ self .check_models_ready ()
188
200
if model_name is None :
189
201
app_label , model_name = app_label .split ('.' )
190
202
return self .get_app_config (app_label ).get_model (model_name .lower ())
@@ -207,10 +219,8 @@ def is_installed(self, app_name):
207
219
Checks whether an application with this name exists in the registry.
208
220
209
221
app_name is the full name of the app eg. 'django.contrib.admin'.
210
-
211
- It's safe to call this method at import time, even while the registry
212
- is being populated. It returns False for apps that aren't loaded yet.
213
222
"""
223
+ self .check_apps_ready ()
214
224
return any (ac .name == app_name for ac in self .app_configs .values ())
215
225
216
226
def get_containing_app_config (self , object_name ):
@@ -221,10 +231,10 @@ def get_containing_app_config(self, object_name):
221
231
222
232
Returns the app config for the inner application in case of nesting.
223
233
Returns None if the object isn't in any registered app config.
224
-
225
- It's safe to call this method at import time, even while the registry
226
- is being populated.
227
234
"""
235
+ # In Django 1.7 and 1.8, it's allowed to call this method at import
236
+ # time, even while the registry is being populated. In Django 1.9 and
237
+ # later, that should be forbidden with `self.check_apps_ready()`.
228
238
candidates = []
229
239
for app_config in self .app_configs .values ():
230
240
if object_name .startswith (app_config .name ):
@@ -297,10 +307,11 @@ def set_installed_apps(self, installed):
297
307
imports safely (eg. that could lead to registering listeners twice),
298
308
models are registered when they're imported and never removed.
299
309
"""
300
- self .check_ready ()
310
+ if not self .ready :
311
+ raise AppRegistryNotReady ("App registry isn't ready yet." )
301
312
self .stored_app_configs .append (self .app_configs )
302
313
self .app_configs = OrderedDict ()
303
- self .ready = False
314
+ self .apps_ready = self . models_ready = self . ready = False
304
315
self .clear_cache ()
305
316
self .populate (installed )
306
317
@@ -309,7 +320,7 @@ def unset_installed_apps(self):
309
320
Cancels a previous call to set_installed_apps().
310
321
"""
311
322
self .app_configs = self .stored_app_configs .pop ()
312
- self .ready = True
323
+ self .apps_ready = self . models_ready = self . ready = True
313
324
self .clear_cache ()
314
325
315
326
def clear_cache (self ):
@@ -402,7 +413,7 @@ def get_app_paths(self):
402
413
warnings .warn (
403
414
"[a.path for a in get_app_configs()] supersedes get_app_paths()." ,
404
415
RemovedInDjango19Warning , stacklevel = 2 )
405
- self .check_ready ()
416
+ self .check_apps_ready ()
406
417
app_paths = []
407
418
for app in self .get_apps ():
408
419
app_paths .append (self ._get_app_path (app ))
0 commit comments