如何在 ModelForm 的 __init__() 方法中修改小部件的属性? [英] How can I modify a widget's attributes in a ModelForm's __init__() method?
本文介绍了如何在 ModelForm 的 __init__() 方法中修改小部件的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想以编程方式修改 Django ModelForm 的 init() 方法中字段的小部件属性.到目前为止,我已经尝试了以下
I want to programatically modify the widget attributes of a field in a Django ModelForm's init() method. Thus far, I've tried the following
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.fields['my_checkbox'].widget_attrs(forms.CheckboxInput(attrs={'onclick':'return false;'}))
不幸的是,这不起作用.有什么想法吗?
Unfortunately, this does not work. Any thoughts?
推荐答案
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.fields['my_checkbox'].widget.attrs['onclick'] = 'return false;'
这篇关于如何在 ModelForm 的 __init__() 方法中修改小部件的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文