栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

django 自定义makemigrations文件

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

django 自定义makemigrations文件

from django.core.management.commands.makemigrations import Command as CoreMakeMigrationsCommand
from django.db.migrations import CreateModel
from django.apps import apps

from utils.custom_model import CommonModel

models = apps.get_models(include_auto_created=False)

use_common_model_name = models = [obj.__name__ for obj in models if
                                  obj._meta.app_label not in ['auth', 'contrib', 'contenttypes', 'sessions', 'messages',
                                                              'staticfiles']
                                  and obj.__base__.__name__ == 'CommonModel']

common_model_field = [i.name for i in CommonModel._meta.get_fields()]


class Command(CoreMakeMigrationsCommand):
    def handle(self, *args, **options):
        print('Overriding default handle-----------------app-user')
        super(Command, self).handle(*args, **options)

    def write_migration_files(self, changes):
        for item in changes.values():
            print('Overriding default names of migrations supplied by django core-----------app-user')
            print('item', item)
            # item[0].name = item[0].name.split('_')[0]
            # print('first value', dir(item[0]))

            for migrate in item:
                models = migrate.operations
                print(f'models is {models}')
                for model in models:
                    if not isinstance(model, CreateModel):
                        print('不是CreateModel')
                        continue
                    if model.name not in use_common_model_name:
                        print('不是继承类-{}'.format(model.name))
                        continue

                    print(f'dir is {dir(model)}')
                    print(f'name is {model.name} type is {type(model.name)} ---------')
                    print(f'fields is {model.fields} type is {type(model.fields)} ---------')

        # super().write_migration_files(changes)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/977083.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号