例:
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)