django中的models类自动转换为编写文档时的格式

例:

a = models.IntegerField(help_text='aaaa', null=False, default=0, choices=MEDICAL_OFFICE,verbose_name=_('aaaa'),)
b = models.IntegerField(help_text='bbbb', null=False, default=0, choices=MEDICAL_OFFICE, verbose_name=_('bbbb'),)

经过转换之后变为

a|int|aaaa
b|int|bbbb

省去了很多时间

源码中的model_trans为你想要转换的model内容
会输出一个model文件里面是转换好格式的内容

注意

每次执行文件时需要把model文件清空

源码

import re

with open('model_trans') as f:
    for x in f:
        if "= models" not in x:
            continue
        type = re.findall(r".*?models\.(.*?)\(.*?", x)[0]
        if type == 'OneToOneField' or type == 'ForeignKey' or type == "IntegerField":
            type = 'int'
        elif type == 'TextField' or type == 'DateField' or type == 'DateTimeField' \
                or type == 'TimeField' or type == 'CharField':
            type = 'str'
        elif type == 'BooleanField':
            type = 'bool'
        elif type == "ManyToManyField":
            type = "list"
        else:
            type = '请自己填写'
        model = re.findall(r"(\w+) =.*?help_text.*?'(.*?)'", x)
        if model:
            pass
        else:
            model = re.findall(r'(\w+) =.*?help_text.*?"(.*?)".*?\n', x)
        try:
            with open("model", 'a') as p:
                p.write(model[0][0]+'|'+type+'|'+model[0][1]+'\n')
        except:
            model = re.findall(r"(\w+) =.*?verbose_name.*?'(.*?)'", x)
            if model:
                pass
            else:
                model = re.findall(r'(\w+) =.*?verbose_name.*?"(.*?)".*?\n', x)
            if model:
                with open("model", 'a') as p:
                    p.write(model[0][0] + '|' + type + '|' + model[0][1] + '\n')
            else:
                print("这句没被提取\t"+x)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值