这是我的forms.py文件:
class MyForm(forms.Form):
data = forms.CharField()
def clean(self):
if any(self.errors):
return
value = self.cleaned_data['data']
if int(value) % 2 != 0:
raise forms.ValidationError(_("Please enter an even number"))
这是我的views.py文件:
def modelform(request):
AuthorFormSet = formset_factory(MyForm, formset=BaseAuthorFormSet, extra=3)
if request.method == 'POST':
forms = AuthorFormSet(request.POST)
if forms.is_valid():
#forms.save() --> IS THERE A SAVE() METHOD ON FORMSET ?
return HttpResponse("Saved successfullly")
else:
forms = AuthorFormSet()
context = {
'form' : forms,
'non_form_errors' : forms.non_form_errors(),
}
return render(request, 'modelform.html', context)
我想知道的是,我们可以像对于formset的views.py文件那样执行'forms.save()'
之类的操作吗?
如果它只是一个表单,我们可以做保存方法吗?
答案 0 :(得分:0)
你是对的,formset有save()
方法所以只需要调用:
if forms.is_valid():
forms.save()
查看关于use model formset in a view的django doc。