Django form表单中forms.TypedChoiceField choices动态赋值

3268阅读 0评论2012-12-19 sunisD0wn
分类:系统运维

在项目中需要重数据库中读取数据然后生成forms.TypedChoiceField

在实际过程中是提交数据库后,刷新网页,forms.TypedChoiceField中的内容不会发生改变,需要重启django才会生效。

解决方法是initial。

在views里面重新为choice赋值 

如下例
max_number = forms.ChoiceField(widget = forms.Select(), choices = ([('1','1'), ('2','2'),('3','3'), ]), initial='3', required = True,)




yourFormInstance = YourFormClass() yourFormInstance.fields['max_number'].choices = [(1,1),(2,2),(3,3)] yourFormInstance.fields['max_number'].initial = [1]
上一篇:Django post 提交报错
下一篇:Django render_to_response报错pop expected at least 1 arguments, got 0