0

如以下代码所示,一旦验证,我将尝试保存表单集。但我总是object has no attribute 'save'出错。如果有人可以帮助我FormSetView使用表单来保存和更新数据,那就太好了。

from extra_views import FormSetView
from foo.forms import MyForm

class MyFormSetView(FormSetView):
    template_name = 'myformset.html'
    form_class = MyForm
    success_url = 'success/'

    def formset_valid(self, formset):
        formset.save()
        return super(MyFormSetView, self).formset_valid(formset)
4

1 回答 1

0

您应该使用ModelFormSetView然后覆盖您的小部件:

class MyForm(forms.ModelForm):
    model = customer

    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.fields['birthday'].widget = birthdaySelector()


class MyFormSetView(ModelFormSetView):
    template_name = 'myformset.html'
    model = customer
    success_url = 'success/'
    form_class = MyForm

无需定义formset_validModelFormSetView自动保存formset。

于 2013-11-09T09:09:49.203 回答