在 django 表单中设置下拉菜单的默认值
我无法在加载表单时设置下拉菜单的默认值。
的代码
state = forms.TypedChoiceField(choices = formfields.State)
State = (
('QC_APPROVED','QC_APPROVED'),
('REVERT','REVERT'),
('FIXED','FIXED'),
)
这是如果我想将默认状态设置为“固定” 。我正在编写这段代码
state = forms.TypedChoiceField(choices = formfields.State, default = 'FIXED')
如果我执行上面的代码,我会收到以下错误。
Exception Value: __init__() got an unexpected keyword argument 'default'
有人可以帮忙解决这个问题吗?
I am unable to set default value for a dropdown while loading forms.
Here is the code
state = forms.TypedChoiceField(choices = formfields.State)
State = (
('QC_APPROVED','QC_APPROVED'),
('REVERT','REVERT'),
('FIXED','FIXED'),
)
If I want to make the default state as FIXED. I am writing this code
state = forms.TypedChoiceField(choices = formfields.State, default = 'FIXED')
If I execute the above code I am getting the below error.
Exception Value: __init__() got an unexpected keyword argument 'default'
Can some one help on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如文档所示: http://docs.djangoproject.com/en /dev/ref/forms/fields/#initial
As shown in documentation: http://docs.djangoproject.com/en/dev/ref/forms/fields/#initial
我在寻找如何为外键字段设置 Django 表单的初始“选定”状态时遇到了这个线程,所以我只想补充一下,您可以按如下方式执行此操作:
models.py:
forms.py:
views.py:
关键是设置
initial={'topic': topic.id}
,但没有详细记录在我看来。I came across this thread while looking for how to set the initial "selected" state of a Django form for a foreign key field, so I just wanted to add that you do this as follows:
models.py:
forms.py:
views.py:
The key is setting
initial={'topic': topic.id}
which is not well documented in my opinion.字段采用
initial
值fields take
initial
values如果其他解决方案不适合你,试试这个:
原来ModelChoiceField有一个名为empty_label的属性。使用空_label你可以输入一个默认值供用户查看。
示例:在 forms.py 中
If the other solutions dont work for you,Try this:
It turns out that ModelChoiceField has an attribute called empty_label.With empty _label you can enter a default value for the user to see.
Example: In forms.py
试试号码:
Try the number: