本文整理汇总了Python中django.db.models.base.ModelBase.add_to_class方法的典型用法代码示例。如果您正苦于以下问题:Python ModelBase.add_to_class方法的具体用法?Python ModelBase.add_to_class怎么用?Python ModelBase.add_to_class使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models.base.ModelBase
的用法示例。
在下文中一共展示了ModelBase.add_to_class方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_to_class
# 需要导入模块: from django.db.models.base import ModelBase [as 别名]
# 或者: from django.db.models.base.ModelBase import add_to_class [as 别名]
def add_to_class(cls, name, value):
# Auto-specify a field's db_column with the table name on the front.
if isinstance(value, models.Field):
db_column_parts = [cls.__name__.lower(), value.db_column or name]
if isinstance(value, models.ForeignKey):
db_column_parts.append('id')
value.db_column = '_'.join(db_column_parts)
ModelBase.add_to_class(cls, name, value)
示例2: generate_child_partition
# 需要导入模块: from django.db.models.base import ModelBase [as 别名]
# 或者: from django.db.models.base.ModelBase import add_to_class [as 别名]
def generate_child_partition(parent, num):
opts = parent._meta
partition_name = '%s_Partition%s' % (parent.__name__, num)
# HACK: Attempting to initialize a model twice results in a broken model
# even though ModelBase is supposed to handle this case already. Instead,
# we explicitly check to make sure the model wasn't created yet by
# using get_model to prevent this case.
app_label = parent._meta.app_label
m = loading.get_model(app_label, partition_name, seed_cache=False)
if m is not None:
return m
partition = ModelBase(partition_name, (parent,), {
'__module__': parent.__module__,
'objects': Manager(),
'Meta': type('Meta', (object,), {
'managed': True,
'db_table': '%s_%s' % (parent._meta.db_table, num),
'unique_together': opts.unique_together,
}),
'_shards': ShardOptions(parent=parent, num=num),
})
partition.add_to_class('DoesNotExist', subclass_exception('DoesNotExist', (parent.DoesNotExist,), parent.__module__))
partition.add_to_class('MultipleObjectsReturned', subclass_exception('MultipleObjectsReturned', (parent.MultipleObjectsReturned,), parent.__module__))
# Connect signals so we can re-send them
signaler = resend_signal(parent)
for signal in (signals.pre_save, signals.post_save, signals.pre_delete, signals.post_delete,
signals.pre_init, signals.post_init, signals.m2m_changed):
signal.connect(signaler, sender=partition, weak=False)
# Ensure the partition is available within the module scope
module = sys.modules[parent.__module__]
setattr(module, partition.__name__, partition)
# Register all partitions with Django
loading.register_models(app_label, partition)
return partition