为Django表单设置下拉菜单的默认值 [英] Set default value for dropdown in django forms

查看:310
本文介绍了为Django表单设置下拉菜单的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

加载表格时,我无法为下拉菜单设置默认值。

I am unable to set default value for a dropdown while loading forms.

这是代码

state = forms.TypedChoiceField(choices = formfields.State)

State = (
         ('QC_APPROVED','QC_APPROVED'),
         ('REVERT','REVERT'),
         ('FIXED','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'

有人可以帮忙吗?

推荐答案

state = forms.TypedChoiceField(choices=formfields.State, initial='FIXED')

如文档所示:< a href = http://docs.djangoproject.com/en/dev/ref/forms/fields/#initial rel = noreferrer> http://docs.djangoproject.com/en/dev/ref/forms / fields /#initial

这篇关于为Django表单设置下拉菜单的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆