python - 覆盖 clean(),数据不可访问

标签 python django django-forms

我想扩展现有表单,以便在提交时发送电子邮件:

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'] 无法从父类(super class)访问,我收到一个 KeyError。我需要做什么?

编辑

我认为我把事情过于简单化了。代码已更新,我认为问题可能是双重子类化。

最佳答案

您需要从基类方法和子类方法返回self.cleaned_data

关于python - 覆盖 clean(),数据不可访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15337918/

相关文章:

python - 使用 0mq (ZeroMQ) 同步两个简单 python3 脚本时出现死锁

python - 在 Python 中搜索两个复杂的正则表达式模式之一而不创建子匹配

python - 使用 pandas dataframe 绘制误差线 matplotlib

Django save() 覆盖,最佳实践 : on model, 表单或 View ?

python - 如何使用 Python 将变量中的列值插入到 Postgres 中?

python - Django 复制/粘贴查询集

python - 备忘单文档 django python

django - nginx,uwsgi,DJango,DEBUG = False时的502, “upstream prematurely closed connection”

python - NoneType' 对象没有属性 'model'

python - 为模型表单额外字段指定小部件(Django)