如何覆盖UpdateView生成的表单的干净方法?

时间:2016-08-10 22:33:16

标签: python django django-views

是否可以覆盖基于类的视图生成的表单的clean method - UpdateView

form中,我会覆盖clean方法来检查第一个字段或第二个字段是否已填充。

表格就像:

class MyForm(forms.ModelForm):
    ...
    def clean(self):
        super(MyForm,self).clean()
        if bool(self.cleaned_data['first_field'])== bool(self.cleaned_data['first_field']):
            raise ValidationError("Please, fill the first or second field")

查看:

class EditOrderView(UpdateView):
    model = Job
    fields = ['language_from', 'language_to', 'level', 'short_description', 'notes',
              'first_field', 'second_field']
    template_name = 'auth/jobs/update-order.html'

    def get_object(self, queryset=None):
        return get_object_or_404(self.model, pk=self.kwargs["pk"], customer=self.request.user)

    def get_success_url(self):
        return '/my-orders/'

    def form_valid(self, form):
        self.order = form.save()
        email.AdminNotifications.edited_order(self.order)
        return HttpResponseRedirect(self.get_success_url())

1 个答案:

答案 0 :(得分:3)

您可以设置form_class

,让您的观点使用您的表单
class EditOrderView(UpdateView):
    model = Job
    form_class = MyForm
    ...