Django, ModelChoiceField() and initial value

后端 未结 6 2121
灰色年华
灰色年华 2020-12-04 12:14

I\'m using something like this:

field1 = forms.ModelChoiceField(queryset=...)

How can I make my form show the a value as selected?

6条回答
  •  被撕碎了的回忆
    2020-12-04 12:35

    You could do this as well:

    form = YourForm(initial = {'field1': pk })
    

    if you are parsing your primary key through a query string or via an ajax call no need for an instance, the query set has already handled that for your drop down, the pk indexes the state you want

提交回复
热议问题