• Django 动态建表


    # Create your models here.
    from django.db import models
    
    
    # name是表名,fields是字段,app_label是你的应用名(如:flow),module是应用下的模型(如:flow.models),options是元类选项
    def create_model1(name, fields=None, app_label='', module='', options=None):
        class Meta:  # 模型类的Meta类
            pass
    
        if app_label:  # 必须在元类中设置app_label,相关属性可参考https://www.cnblogs.com/lcchuguo/p/4754485.html
            setattr(Meta, 'app_label', app_label)  # 更新元类的选项
    
        if options is not None:
            for key, value in options.items():
                setattr(Meta, key, value)  # 设置模型的属性
            attrs = {'__module__': module, 'Meta': Meta}  # 添加字段属性
        if fields:
            attrs.update(fields)  # 创建模型类对象
        return type(name, (models.Model,), attrs)  #用type动态创建类
    
    
    def install(custom_model):
        from django.db import connection
        from django.db.backends.base.schema import BaseDatabaseSchemaEditor
    
        """
        fix:
            editor = BaseDatabaseSchemaEditor(connection)
            try:
                editor.create_model(model=custom_model) # 会抛出个异常,不知为啥,但表会创建
            except AttributeError as error:
                print(error)
            在BaseDatabaseSchemaEditor类中有一个__enter__方法,
            需要通过with上下文打开以后deferred_sql变量才会在实例化后赋值给editor
            这样就不会有'BaseDatabaseSchemaEditor' object has no attribute 'deferred_sql'
            
        """
        with BaseDatabaseSchemaEditor(connection) as editor:
            editor.create_model(model=custom_model)
    
    
    def CreateNewTab(tabdate):
        fields = {
            "name": models.CharField(max_length=30),
            "job_number": models.IntegerField(unique=True),
            "even_shift": models.CharField(max_length=1024),
            "administrator_shift": models.CharField(max_length=1024),
            "middle_shift": models.CharField(max_length=1024),
            "meeting_ops": models.CharField(max_length=1024),
            "meeting_ops_manager": models.CharField(max_length=1024),
            "checking_station": models.CharField(max_length=1024),
            '__str__': lambda self: '%s %s %s %s %s %s %s %s' % (
                self.name,
                self.job_number,
                self.even_shift,
                self.administrator_shift,
                self.middle_shift,
                self.meeting_ops,
                self.meeting_ops_manager,
                self.checking_station,
            ), }
        options = {'ordering': [
            "name",
            "job_number",
            "even_shift",
            "administrator_shift",
            "middle_shift",
            "meeting_ops",
            "meeting_ops_manager",
            "checking_station",
        ], 'verbose_name': 'valued customer', }
        custom_model = create_model1(name=tabdate, fields=fields, options=options, app_label='ops_shift_',
                                     module='flow.models')
        install(custom_model)  # 同步到数据库中
    
    
    

      

  • 相关阅读:
    Android数据存储之IO
    UVA
    【源代码剖析】tornado-memcached-sessions —— Tornado session 支持的实现(二)
    杀毒与免杀技术具体解释之二:特征码定位-工具及原理
    C++ 訪问控制权限图解
    新辰:传统行业进军互联网 怎样颠覆网络获得新生?
    【二】注入框架RoboGuice使用:(Your First View Injection)
    POJ 1741 Tree 树形DP(分治)
    技术单词
    活动合集 | 2017微软技术暨生态大会 英雄讲师召集令
  • 原文地址:https://www.cnblogs.com/randomlee/p/10737181.html
Copyright © 2020-2023  润新知