将自定义表单参数传递给表单集

发布于 2024-08-08 20:45:52 字数 1027 浏览 5 评论 0原文

我定义了以下表单,

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 form
form = MyForm(readOnly=True, instance=ModelA)

but when I try to use it in the inlineformset_factory
Formset = 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 do
Formset = 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扬花落满肩 2024-08-15 20:45:52

深入研究django.forms.models,您可以看到inlineformset_factory需要一个表单,而不是一个实例。这就是为什么你的最后一次尝试有效而另一次失败......传递实例将不起作用。

这应该可以满足您的需求:

class MyReadOnlyForm(MyForm):

    def __init__(self, *args, **kwargs):
        super(MyReadOnlyForm,self).__init__(readOnly=True, *args,**kwargs)


Formset = inlineformset_factory(ModelA, ModelB form=MyReadOnlyForm)

如果您需要两个版本

if read_only is True:
    form_class = MyReadOnlyForm
else:
    form_class = MyForm 

Formset = inlineformset_factory(ModelA, ModelB form=form_class)

Digging through django.forms.models you can see that inlineformset_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:

class MyReadOnlyForm(MyForm):

    def __init__(self, *args, **kwargs):
        super(MyReadOnlyForm,self).__init__(readOnly=True, *args,**kwargs)


Formset = inlineformset_factory(ModelA, ModelB form=MyReadOnlyForm)

If you need both versions

if read_only is True:
    form_class = MyReadOnlyForm
else:
    form_class = MyForm 

Formset = inlineformset_factory(ModelA, ModelB form=form_class)
旧街凉风 2024-08-15 20:45:52

谢谢。我确实在另一篇文章中找到了以下内容,想知道其中一个是否比另一个更好。

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文