mirror of
https://github.com/django/django.git
synced 2025-10-28 08:06:09 +00:00
Added require_ready argument to get_model methods.
This allows bringing back the behavior of Django < 1.7. Also fixed the check for the app registry being ready in AppConfig.get_model(s), which was inconsistent with the equivalent check in Apps.get_model(s). That part is a backwards-incompatible change.
This commit is contained in:
committed by
Tim Graham
parent
fd748c42a9
commit
625cd5bcb3
@@ -155,13 +155,16 @@ class AppConfig(object):
|
||||
# Entry is a path to an app config class.
|
||||
return cls(app_name, app_module)
|
||||
|
||||
def get_model(self, model_name):
|
||||
def get_model(self, model_name, require_ready=True):
|
||||
"""
|
||||
Returns the model with the given case-insensitive model_name.
|
||||
|
||||
Raises LookupError if no model exists with this name.
|
||||
"""
|
||||
self.apps.check_models_ready()
|
||||
if require_ready:
|
||||
self.apps.check_models_ready()
|
||||
else:
|
||||
self.apps.check_apps_ready()
|
||||
try:
|
||||
return self.models[model_name.lower()]
|
||||
except KeyError:
|
||||
|
||||
@@ -177,7 +177,7 @@ class Apps(object):
|
||||
result.extend(list(app_config.get_models(include_auto_created, include_swapped)))
|
||||
return result
|
||||
|
||||
def get_model(self, app_label, model_name=None):
|
||||
def get_model(self, app_label, model_name=None, require_ready=True):
|
||||
"""
|
||||
Returns the model matching the given app_label and model_name.
|
||||
|
||||
@@ -190,10 +190,20 @@ class Apps(object):
|
||||
model exists with this name in the application. Raises ValueError if
|
||||
called with a single argument that doesn't contain exactly one dot.
|
||||
"""
|
||||
self.check_models_ready()
|
||||
if require_ready:
|
||||
self.check_models_ready()
|
||||
else:
|
||||
self.check_apps_ready()
|
||||
|
||||
if model_name is None:
|
||||
app_label, model_name = app_label.split('.')
|
||||
return self.get_app_config(app_label).get_model(model_name.lower())
|
||||
|
||||
app_config = self.get_app_config(app_label)
|
||||
|
||||
if not require_ready and app_config.models is None:
|
||||
app_config.import_models()
|
||||
|
||||
return app_config.get_model(model_name, require_ready=require_ready)
|
||||
|
||||
def register_model(self, app_label, model):
|
||||
# Since this method is called when models are imported, it cannot
|
||||
|
||||
Reference in New Issue
Block a user