Django, ModelChoiceField() and initial value

If you want to set the default initial value you should be defining initial like other form fields except you set it to the id instead.

Say you've got field1 like this:

class YourForm(forms.Form):
    field1 = forms.ModelChoiceField(queryset = MyModel.objects.all() )

then you need to set initial when you create your form like this:

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

rather than:

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

I'm also assuming you've defined __unicode__ for your models so this displays correctly.


You can just use

 field1 = forms.ModelChoiceField(queryset=..., initial=0) 

to make the first value selected etc. It's more generic way, then the other answer.