本文整理汇总了Python中mptt.managers.TreeManager.__class__方法的典型用法代码示例。如果您正苦于以下问题:Python TreeManager.__class__方法的具体用法?Python TreeManager.__class__怎么用?Python TreeManager.__class__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mptt.managers.TreeManager
的用法示例。
在下文中一共展示了TreeManager.__class__方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: register
# 需要导入模块: from mptt.managers import TreeManager [as 别名]
# 或者: from mptt.managers.TreeManager import __class__ [as 别名]
def register(meta, cls, **kwargs):
"""
For the weird cases when you need to add tree-ness to an *existing*
class. For other cases you should subclass MPTTModel instead of calling this.
"""
if not issubclass(cls, models.Model):
raise ValueError, "register() expects a Django model class argument"
if not hasattr(cls, '_mptt_meta'):
cls._mptt_meta = MPTTOptions(**kwargs)
abstract = getattr(cls._meta, 'abstract', False)
try:
MPTTModel
except NameError:
# We're defining the base class right now, so don't do anything
# We only want to add this stuff to the subclasses.
# (Otherwise if field names are customized, we'll end up adding two
# copies)
pass
else:
if not issubclass(cls, MPTTModel):
bases = list(cls.__bases__)
# strip out bases that are strict superclasses of MPTTModel.
# (i.e. Model, object)
# this helps linearize the type hierarchy if possible
for i in range(len(bases)-1, -1, -1):
if issubclass(MPTTModel, bases[i]):
del bases[i]
bases.insert(0, MPTTModel)
cls.__bases__ = tuple(bases)
for key in ('left_attr', 'right_attr', 'tree_id_attr', 'level_attr'):
field_name = getattr(cls._mptt_meta, key)
try:
cls._meta.get_field(field_name)
except models.FieldDoesNotExist:
field = models.PositiveIntegerField(db_index=True, editable=False)
field.contribute_to_class(cls, field_name)
# Add a tree manager, if there isn't one already
if not abstract:
tree_manager_attr = cls._mptt_meta.tree_manager_attr
manager = getattr(cls, tree_manager_attr, None)
if (not manager) or manager.model != cls:
if not manager:
manager = TreeManager()
else:
# manager.model might already be set if this is a proxy model
# (in that case, we need to create a new manager, or _base_manager will be wrong)
manager = manager.__class__()
manager.contribute_to_class(cls, tree_manager_attr)
manager.init_from_model(cls)
setattr(cls, '_tree_manager', manager)
return cls