使用Django Extra Views保存FormSetView

时间:2013-11-08 19:48:04

标签: django django-forms

如下面的代码所示,我试图在验证后保存formset。但我总是得到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)

1 个答案:

答案 0 :(得分: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会自动保存套餐。