本文介绍了如何使用Django choicefield设置动态选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在动态中设置选项。
I want to set choices in dynamic.
我使用__set_choices方法,但是当请求方法为POST时,
is_valid方法始终返回False。 p>
I used __set_choices method but, when request method is POST,is_valid method always return False.
if request.method=='POST':
_form = MyForm(request.POST)
if _form.is_valid():
#something to do
推荐答案
我经常在动态地设置选项构造函数:
I often set the choices dynamicly in the constructor:
class MyForm(BaseForm):
afield = forms.ChoiceField(choices=INITIAL_CHOICES)
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.fields['afield'].choices = my_computed_choices
这篇关于如何使用Django choicefield设置动态选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!