Django將自定義表單參數(shù)傳遞給Formset這是使用form_kwargs在Django 1.9中修復(fù)的。我有一個(gè)看起來(lái)像這樣的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)我用這樣的方式稱(chēng)這個(gè)形式:form = ServiceForm(affiliate=request.affiliate)request.affiliate登錄用戶在哪里。這按預(yù)期工作。我的問(wèn)題是我現(xiàn)在想把這個(gè)單一的表單變成一個(gè)formset。我無(wú)法弄清楚的是,在創(chuàng)建formset時(shí),我如何將聯(lián)盟信息傳遞給各個(gè)表單。根據(jù)文檔制作一個(gè)formset,我需要做這樣的事情:ServiceFormSet = forms.formsets.formset_factory(ServiceForm, extra=3)然后我需要像這樣創(chuàng)建它:formset = ServiceFormSet()現(xiàn)在,我如何通過(guò)這種方式將affiliate = request.affiliate傳遞給單個(gè)表單?
Django將自定義表單參數(shù)傳遞給Formset
慕運(yùn)維8079593
2019-08-06 14:42:56