我们可以在django 1.9中向modelformset_factory发送请求对象或关键字参数吗?

时间:2016-09-06 09:24:08

标签: python django django-forms modelform

我有以下观点

WHERE

但是我想通过使用我们传递的请求或关键字参数中的一些值(如IN(10,19,23,24,28,33,45,46,1,4,7,8,12,16,18,22,29)

来在形式的 init 方法中动态地使用FirmPartnerForm中的某些字段

那么可以将请求对象或任何关键字参数发送到modelformset_factory吗?如下

def application(request, uuid):
    application = get_object_or_404(LoanApplication, uuid=uuid)

    partners = FirmPartner.objects.filter(application=application)
    PartnerFormset = modelformset_factory(FirmPartner, form=FirmPartnerForm, can_delete=True, extra=1)

    if request.method == 'POST':
        .........
        ..........

修改

现在我升级到django 1.9.9及以下是我的代码

modelformset_factory(......application=application)

forms.py

modelformset_factory(FirmPartner, form=FirmPartnerForm, can_delete=True, extra=1, request=request, application=application) ?

错误追溯

from .forms import FirmPartnerForm

PartnerFormset = modelformset_factory(FirmPartner, form=FirmPartnerForm, can_delete=True, extra=1,
                       form_kwargs={'request': request, 'application': application})

所以在class FirmPartnerForm(forms.ModelForm): class Meta: model = FirmPartner exclude = ['application'] def __init__(self, *args, **kwargs): super(FirmPartnerForm, self).__init__(*args, **kwargs) if kwargs.get('application', None): application = kwargs.get('application ..................... ..................... 表示在1.9中添加了form_kwargs之后,我已升级到1.9.9但仍然出现上述错误?

1 个答案:

答案 0 :(得分:1)

您需要form_kwargs参数。

modelformset_factory(FirmPartner, form=FirmPartnerForm, can_delete=True, extra=1,
                     form_kwargs={'request': request, 'application': application})