【发布时间】:2015-05-04 20:31:56
【问题描述】:
我在 ModelForm 中有一个字段在模型中不存在。该字段通过覆盖 ModelForm.save() 函数单独保存:
class MyForm(ModelForm):
custom_field = CharField(label='Custom field', widget=TextInput())
def save(self, commit=True):
data = self.cleaned_data
# save custom_field
现在如何在渲染之前将此自定义字段加载到 ModelForm(在编辑模式下)。我尝试设置字段字典,但 ModelForm 中不存在字段字典:
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.fields['custom_field'] = self.instance._meta.model.custom_field
我收到以下错误:
type object 'MyForm' has no attribute 'fields'
【问题讨论】:
-
你能显示你的表单域吗?