本文整理汇总了Python中django.apps.registry.Apps.populate_models方法的典型用法代码示例。如果您正苦于以下问题:Python Apps.populate_models方法的具体用法?Python Apps.populate_models怎么用?Python Apps.populate_models使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.apps.registry.Apps
的用法示例。
在下文中一共展示了Apps.populate_models方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ProjectState
# 需要导入模块: from django.apps.registry import Apps [as 别名]
# 或者: from django.apps.registry.Apps import populate_models [as 别名]
class ProjectState(object):
"""
Represents the entire project's overall state.
This is the item that is passed around - we do it here rather than at the
app level so that cross-app FKs/etc. resolve properly.
"""
def __init__(self, models=None):
self.models = models or {}
self.apps = None
def add_model_state(self, model_state):
self.models[(model_state.app_label, model_state.name.lower())] = model_state
def clone(self):
"Returns an exact copy of this ProjectState"
return ProjectState(
models=dict((k, v.clone()) for k, v in self.models.items())
)
def render(self):
"Turns the project state into actual models in a new Apps"
if self.apps is None:
self.apps = Apps()
# Populate the app registry with a stub for each application.
app_labels = set(model_state.app_label for model_state in self.models.values())
app_configs = [AppConfigStub(label) for label in sorted(app_labels)]
self.apps.populate_apps(app_configs)
self.apps.populate_models()
# We keep trying to render the models in a loop, ignoring invalid
# base errors, until the size of the unrendered models doesn't
# decrease by at least one, meaning there's a base dependency loop/
# missing base.
unrendered_models = list(self.models.values())
while unrendered_models:
new_unrendered_models = []
for model in unrendered_models:
try:
model.render(self.apps)
except InvalidBasesError:
new_unrendered_models.append(model)
if len(new_unrendered_models) == len(unrendered_models):
raise InvalidBasesError("Cannot resolve bases for %r" % new_unrendered_models)
unrendered_models = new_unrendered_models
return self.apps
@classmethod
def from_apps(cls, apps):
"Takes in an Apps and returns a ProjectState matching it"
app_models = {}
for model in apps.get_models():
model_state = ModelState.from_model(model)
app_models[(model_state.app_label, model_state.name.lower())] = model_state
return cls(app_models)
def __eq__(self, other):
if set(self.models.keys()) != set(other.models.keys()):
return False
return all(model == other.models[key] for key, model in self.models.items())
def __ne__(self, other):
return not (self == other)