我想扩展现有表单,以便在提交时发送电子邮件:
class BaseForm(forms.Form):
field = forms.CharField()
def clean(self):
"do stuff"
return self.cleaned_data
class MyForm(BaseForm):
def clean_field(self):
"do some validation"
return self.cleaned_data['field']
class MyEmailingForm(MyForm):
def clean(self):
super(MyEmailingForm, self).clean()
send_mail('Form data',
self.cleaned_data['field'],
'Form <noreply@myform.co.uk>',
["formdata@myform.co.uk"],
fail_silently=True)
问题是self.cleaned_data['field']
无法从超类访问,我得到一个KeyError
. 我需要做什么?
编辑
我想我把事情过于简单化了。代码已经更新,我认为问题可能是双重子类化。