Django formset:POST formset

时间:2011-10-20 15:17:28

标签: django django-forms django-views

我的django formset有问题。我想验证formset中的一个字段。我按照这些链接 Django Passing Custom Form Parameters to Formset

并且能够创建表单。

但是如何将我的数据发布到formset。

这是我的表格

class BookingDetailsForm(forms.Form):
    age = forms.IntegerField()

   def __init__(self, trip, *args, **kwargs):
        super(BookingDetailsForm, self).__init__(*args, **kwargs)
        age = self.fields["age"]
        trip = trip
        print "This prints when formset is created"
    def clean_age(self):
        // want some checking here
        #raise ValidationError('Error msg')
        return self.cleaned_data['age']

我的观点是

formset = formset_factory(BookingDetailsForm, extra=number_of_peoples)
formset.form = staticmethod(curry(BookingDetailsForm, trip))
// above two line is working perfectly

然后如何发布我的formset?

formset = formset_factory(BookingDetailsForm, trip, request.POST)// not working

发布后我想用干净的方法检查年龄字段。

1 个答案:

答案 0 :(得分:3)

formset_class = formset_factory(BookingDetailsForm, extra=number_of_peoples)
formset_class.form = staticmethod(curry(BookingDetailsForm, trip))
formset = formset_class(request.POST)