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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章