According to the documentation providing initial values for fields that are bound to a model is not possible.
In my model form though I have created an additional unbound field:
class DealCForm(ModelForm):
attach_deal_conversation = forms.BooleanField(required=False, initial=False)
Hence I would like to set this value if certain conditions are met.
View:
deal_formset = modelformset_factory(Deal, form=DealCForm, extra=0)
if (request.POST)
pass
else:
opendeal_formset = deal_formset(queryset=formset_query)
variables = RequestContext(request, {'opendeal_formset' : opendeal_formset)
return render_to_response('conversation.html', variables)
In the view, just before sending it to the template, I have set the value directly, however it doesn’t work:
for dfm in deal_formset:
for odfm in opendeal_formset:
if dfm.pk == odfm.pk:
odfm.attach_deal_conversation = True;
But it doesn’t work. ANy idea how to set the initial value for an unbound field?
Many Thanks
This runs for me: