Select2将不会显示在Django表单上设置的初始值

问题描述

我一直在处理select2和django表单,我想显示要选择的模型的第一个对象,以供我使用以下代码

    class SchoolForm(forms.Form):
        school = forms.ModelChoiceField(
        label=_('Campus'),queryset=School.objects.none(),required=False,)

    def __init__(self,*args,**kwargs):
        self.school_sys = kwargs.pop("school")
        super(SchoolForm,self).__init__(*args,**kwargs)

        school_campus = self.school_sys.school_set.all()

        self.fields['school'].queryset = school_campus

        if school_campus.exists():
           self.fields['school'].initial = school_campus.first()

and spit this to the html however the select2 is showing all school but no initial first value on select2

还是我可以在jquery中做到这一点?谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)