Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
当我使用模型表单时,我可以通过使用 Meta 类和exclude来排除显示的字段。但是,对于标准的非模型形式,这不起作用。我想隐藏一个字段调用量并在初始化时填充。如何?
amount = forms.FloatField()
好吧,我试图首先排除这样的......
class Meta: exclude = ('amount',)
但这似乎不适用于任何模型形式。
用于forms.ModelForm覆盖初始化
forms.ModelForm
class YourForm(forms.ModelForm): amount = forms.FloatField() def __init__(self, *args, **kwargs): super(YourForm, self).__init__(*args, **kwargs) self.fields['amount'].widget = forms.HiddenInput() self.fields['amount'].initial = ''