python

超轻量级php框架startmvc

django表单实现下拉框的示例讲解

更新时间:2020-06-04 10:12 作者:startmvc
方法一:queue=forms.ModelChoiceField(label=u'队列',queryset=Queue.objects.all())方法二:classServerForm(forms.

方法一:


queue = forms.ModelChoiceField(label=u'队列',queryset=Queue.objects.all())

方法二:


 class ServerForm(forms.Form):
 queue = forms.ChoiceField(label=u'队列')
 def __init__(self,*args,**kwargs):
 super(ServerForm,self).__init__(*args,**kwargs)
 self.fields['queue'].choices=((x.que,x.disr) for x in Queue.objects.all())

注意:在Queue模型的__unicode__()函数中返回你下拉菜单需要显示的字段

以上这篇django表单实现下拉框的示例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。