最近对django真是大爱呀,哈哈。公司有个需求是为一些素材动态生成属性字段,所以需要django来动态生成表单。
一开始的写法如下:
class AddObjForm(forms.Form):
id = forms.CharField()
def __init__(self, *args, **kwargs):
mdfields = copy.deepcopy(kwargs['mdfields'])
del kwargs['mdfields']
super(AddObjForm, self).__init__(*args, **kwargs)
if mdfields is not None:
for f in mdfields:
exestr = ""
if f.showtype == 0:
exestr = 'self.'+f.name+"=forms.CharField()"
else:
exestr = 'self.'+f ...