def _find_installed_apps_entry(module_label):
"""
Given a module label, finds the best matching INSTALLED_APPS entry.
This is made trickier by the fact that we don't know what part of the
module_label is part of the INSTALLED_APPS entry. So we try all possible
combinations, trying the longer versions first. E.g. for
'dashboard.catalogue.forms', 'dashboard.catalogue' is attempted before
'dashboard'
"""
modules = module_label.split('.')
# if module_label is 'dashboard.catalogue.forms.widgets', combinations
# will be ['dashboard.catalogue.forms', 'dashboard.catalogue', 'dashboard']
combinations = [
'.'.join(modules[:-count]) for count in range(1, len(modules))]
for app_name in combinations:
entry = _get_installed_apps_entry(app_name)
if entry:
return entry, app_name
raise AppNotFoundError(
"Couldn't find an app to import %s from" % module_label)
评论列表
文章目录