Django - 在表单__init__中调用super,在“GET”上启动表单验证

时间:2013-07-19 06:54:59

标签: django forms

我的应用程序中有一个ModelForm,我想修改 init 函数来添加一些自定义。

init 被注释掉时,表单正常工作并验证。当我覆盖init并转到表单呈现的url时,它会自动显示“Field xyz is required”

问题的原因是什么?

class CreateListView(FormMixin, ListView):

    def get_context_data(self, **kwargs):
        self.object_list = self.get_queryset()
        data = super(ListView, self).get_context_data()
        data['object_list'] = self.get_queryset()
        data['form'] = self.get_form(self.form_class)
        return data

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        if form.is_valid():
            form = form.save()
            return HttpResponseRedirect(form.get_absolute_url())
        return self.form_invalid(self.get_context_data())



class ActionGroupForm(forms.ModelForm):

    class Meta:
        model = ActionGroup

    def __init__(self, *args, **kwargs):
        super(ActionGroupForm, self).__init__(args, kwargs)

1 个答案:

答案 0 :(得分:6)

您遗失***

super(ActionGroupForm, self).__init__(*args, **kwargs)