当前位置: 首页>>代码示例>>Python>>正文


Python ModelBase.add_to_class方法代码示例

本文整理汇总了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)
开发者ID:markpasc,项目名称:bee,代码行数:10,代码来源:import_mt.py

示例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
开发者ID:SpringAutumn,项目名称:sharding-example,代码行数:42,代码来源:models.py


注:本文中的django.db.models.base.ModelBase.add_to_class方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。