Django将自定义表单参数传递给modelformset_factory

时间:2014-07-02 14:07:05

标签: python django forms formset

根据关于formset_factory的this回答,我试图对modelformset_factory做同样的事情:

from django.utils.functional import curry
from functools wraps

AccountMemberFormSetBase = modelformset_factory(AccountMember,
                                                form=wraps(AccountMemberLimitedModelForm)(curry(AccountMemberLimitedModelForm, affiliate="test")),
                                                extra=2)

这会引发以下错误:

function() argument 1 must be code, not str
Exception Location: ../django/forms/models.py in modelform_factory, line 528

这里有什么想法吗?

2 个答案:

答案 0 :(得分:1)

我今天发现自己处于同样的境地。

有关详细信息和解决方法,请参阅Django Passing Custom Form Parameters to Formset中的评论(我希望可以使用指向StackOverflow的链接)。

答案 1 :(得分:0)

class AccountMemberLimitedModelForm(forms.Form):
...     def __init__(self, *args, **kwargs):
...         affiliate = kwargs.pop('affiliate')
...         super(AccountMemberLimitedModelForm, self).__init__(*args, **kwargs)



AccountMemberFormSetBase = formset_factory(AccountMemberLimitedModelForm)
    formset = AccountMemberFormSetBase(form_kwargs={'affiliate': "test"})