是否可以覆盖基于类的视图生成的表单的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())
答案 0 :(得分:3)
您可以设置form_class
。
class EditOrderView(UpdateView):
model = Job
form_class = MyForm
...