Django, ModelChoiceField() and initial value

后端 未结 6 2106
灰色年华
灰色年华 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:43

    The times they have changed:

    The default initial value can now be set by defining initial like other form fields except you set it to the id instead.

    Now this will suffice:

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

    Though both still work.

提交回复
热议问题