将自定义表单参数传递给表单集
我定义了以下表单,
class MyForm(ModelForm):
def __init__(self, readOnly=False, *args, **kwargs):
super(MyForm,self).__init__(*args,**kwrds)
if readOnly:
Do stuff to make the inputs readonly
当我在视图中将其实例化为表单时,MyForm 可以完美运行form = MyForm(readOnly=True, instance=ModelA)
但是当我尝试在 inlineformset_factory 中使用它时Formset = inlineformset_factory(ModelA, ModelB form=MyForm(readOnly=True))
我收到错误“NoneType 对象不可调用。”
我认为这是因为表单是在没有模型实例的情况下初始化的
因为 MyForm 在内联中初始化,
我知道问题是我在内联调用中使用 MyForm 的方式
因为如果执行以下任一操作,我会收到相同的错误
Formset = inlineformset_factory(ModelA, ModelB form=MyForm(readOnly=True))
Formset = inlineformset_factory(ModelA, ModelB form=MyForm())
但如果我这样做它就可以Formset = inlineformset_factory(ModelA, ModelB form=MyForm)
显然 readOnly 参数默认为 False 并且我的输入没有更改。 有谁知道如何使用 inlineformset_factory 将 readOnly 参数传递给 MyForm 或者如何实现我想要的?
谢谢 安德鲁
I have the following Form defined
class MyForm(ModelForm):
def __init__(self, readOnly=False, *args, **kwargs):
super(MyForm,self).__init__(*args,**kwrds)
if readOnly:
Do stuff to make the inputs readonly
MyForm works perfectly when I instantiate it in the view as a formform = MyForm(readOnly=True, instance=ModelA)
but when I try to use it in the inlineformset_factoryFormset = inlineformset_factory(ModelA, ModelB form=MyForm(readOnly=True))
I get the error "NoneType object is not callable."
I think this is because the form is being initialised without a model instance
because MyForm is being initialised within the inline
I know the problem is the way I am using the MyForm in the inline call
because I get the same error if I do either of the following
Formset = inlineformset_factory(ModelA, ModelB form=MyForm(readOnly=True))
Formset = inlineformset_factory(ModelA, ModelB form=MyForm())
but it works if I doFormset = inlineformset_factory(ModelA, ModelB form=MyForm)
obviously the readOnly param defaults to False and my inputs are not changed.
Does anyone know how I can pass the readOnly param to MyForm using the inlineformset_factory or how else I can achieve what I want?
Thanks
Andrew
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
深入研究
django.forms.models
,您可以看到inlineformset_factory
需要一个表单类,而不是一个实例。这就是为什么你的最后一次尝试有效而另一次失败......传递实例将不起作用。这应该可以满足您的需求:
如果您需要两个版本
Digging through
django.forms.models
you can see thatinlineformset_factory
needs a form class, not an instance. This is why your last try works and the other fail...passing in an instance won't work.This should give you what you are looking for:
If you need both versions
谢谢。我确实在另一篇文章中找到了以下内容,想知道其中一个是否比另一个更好。
Formset = inlineformset_factory(ModelA, ModelB form=MyForm)
Formset.form = staticmethod(curry(MyForm, reaOnly=readOnlyvalue))
myFormset = Formset(request.Files, instance=modelAInst)
Thanks. I did find the following in another post and was wondering if one was better than the other.
Formset = inlineformset_factory(ModelA, ModelB form=MyForm)
Formset.form = staticmethod(curry(MyForm, reaOnly=readOnlyvalue))
myFormset = Formset(request.Files, instance=modelAInst)