0
這是我forms.py文件:'formset'或'form'上有'save'方法嗎? (不是的ModelForm或modelformfactory)
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)
我想知道的是,我們可以這樣做'forms.save()'
與viewset.py文件中的formset相同?
如果只是一個表單,我們可以執行保存方法嗎?
我不是在問一個'modelformset',我只是在問一個'formset'。 –