Django:如何在构建表单之后添加一个额外的表单? [英] Django: How to add an extra form to a formset after it has been constructed?
问题描述
这大概是我想要做的:
def post(request):
VehicleFormSet = formset_factory(StaffVehicleForm)
如果request.method =='POST':
vehicle_formset = VehicleFormSet(request.POST)
如果在request.POST中为'add_vehicle'.POST:
如果为vehicle_formset。 is_valid():
form_count = vehicle_formset.total_form_count()
vehicle_formset.forms.append(vehicle_formset._construct_form(form_count))
基本上,如果用户点击添加按钮,并且其条目有效,我想将另一个空白表单添加到表单集中,并隐藏上一个。
上面的代码的问题是我无法弄清楚如何增加 total_form_count()
。我现在有这样的方式,它将一直工作 ,然后如果再次按下它,则不会发生任何事情,大概是因为 form_count
是一样的。我也不喜欢调用 _construct_form
并依赖于内部。
pre> class RequiredFormSet(BaseFormSet):
def add_form(self,** kwargs):
#添加窗体
tfc = self.total_form_count()
self.forms.append(self._construct_form(tfc,** kwargs))
self.forms [tfc] .is_bound = False
#使数据可变
self.data = self.data.copy()
#增加隐藏表单计数
total_count_name ='%s-%s'%(self.management_form.prefix,TOTAL_FORM_COUNT)
initial_count_name ='%s-%s'%(self.management_form.prefix,INITIAL_FORM_COUNT)
self.data [total_count_name] = self.management_form.cleaned_data [TOTAL_FORM_COUNT] + 1
self.data [ initial_count_name] = self.management_form.cleaned_data [INITIAL_FORM_COUNT] + 1
def add_fields(self,form,index):
super (RequiredFormSet,self).add_fields(form,index)
form.empty_permitted = False
那会做到只花了7个小时才弄清楚。而我仍然不知道为什么我需要 .is_bound = False
使初始值不会松动。
This is roughly what I'm trying to do:
def post(request):
VehicleFormSet = formset_factory(StaffVehicleForm)
if request.method == 'POST':
vehicle_formset = VehicleFormSet(request.POST)
if 'add_vehicle' in request.POST:
if vehicle_formset.is_valid():
form_count = vehicle_formset.total_form_count()
vehicle_formset.forms.append(vehicle_formset._construct_form(form_count))
Basically, if a user clicks the "Add" button and their entry is valid, I want to add another blank form to the formset, and hide the previous one.
The problem with the code above is that I can't figure out how to increase total_form_count()
. The way I have it now, it will work once, and then if you press it again, nothing will happen, presumably because form_count
is the same. I also don't like calling _construct_form
and relying on the internals.
class RequiredFormSet(BaseFormSet):
def add_form(self, **kwargs):
# add the form
tfc = self.total_form_count()
self.forms.append(self._construct_form(tfc, **kwargs))
self.forms[tfc].is_bound = False
# make data mutable
self.data = self.data.copy()
# increase hidden form counts
total_count_name = '%s-%s' % (self.management_form.prefix, TOTAL_FORM_COUNT)
initial_count_name = '%s-%s' % (self.management_form.prefix, INITIAL_FORM_COUNT)
self.data[total_count_name] = self.management_form.cleaned_data[TOTAL_FORM_COUNT] + 1
self.data[initial_count_name] = self.management_form.cleaned_data[INITIAL_FORM_COUNT] + 1
def add_fields(self, form, index):
super(RequiredFormSet, self).add_fields(form, index)
form.empty_permitted = False
That will do it. Only took 7 hours to figure out. And I still don't know why I need .is_bound = False
to make the initial values not screw up.
这篇关于Django:如何在构建表单之后添加一个额外的表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!