Skip to main content
 首页 » 编程设计

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

2025年05月04日37yxwkf

基于 this关于 formset_factory 的回答,我尝试对 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 

知道这里出了什么问题吗?

请您参考如下方法:

今天我发现自己处于同样的情况。

请参阅我在 Django Passing Custom Form Parameters to Formset 中的评论有关详细信息和解决方法(我希望 StackOverflow 本身的链接是可以接受的)。