【发布时间】:2010-10-12 00:00:16
【问题描述】:
这已在 Django 1.9 中通过form_kwargs 修复。
我有一个如下所示的 Django 表单:
class ServiceForm(forms.Form):
option = forms.ModelChoiceField(queryset=ServiceOption.objects.none())
rate = forms.DecimalField(widget=custom_widgets.SmallField())
units = forms.IntegerField(min_value=1, widget=custom_widgets.SmallField())
def __init__(self, *args, **kwargs):
affiliate = kwargs.pop('affiliate')
super(ServiceForm, self).__init__(*args, **kwargs)
self.fields["option"].queryset = ServiceOption.objects.filter(affiliate=affiliate)
我这样称呼这个表格:
form = ServiceForm(affiliate=request.affiliate)
request.affiliate 是登录用户。这按预期工作。
我的问题是我现在想把这个单一的表单变成一个表单集。我想不通的是如何在创建表单集时将附属信息传递给各个表单。根据文档制作表单集,我需要做这样的事情:
ServiceFormSet = forms.formsets.formset_factory(ServiceForm, extra=3)
然后我需要像这样创建它:
formset = ServiceFormSet()
现在我如何通过这种方式将affiliate=request.affiliate 传递给各个表单?
【问题讨论】:
标签: python django forms django-forms