Reputation: 21
Can someone help me here?
My form looks like this:
class RecieveLineForm(forms.ModelForm):
purchaseorderline = forms.IntegerField(widget=forms.HiddenInput())
rl_quantity = forms.IntegerField(label='Quantity')
class Meta:
model = RecieveLine
Now, I need to set a default value for my purchaseorderline which is originally a ModelChoice, but I overridden it as IntegerField because I'm planning to hide it so that whenever I get its data, it will return an integer and not an Object. Anyway, I need to know how to override the default value of it. Must I use the __init__
? My problem there is that, I don't know how to override the default value after it's been set. Any suggestions?
z
Upvotes: 2
Views: 1156
Reputation: 123488
You should be able to set this in the declaration using the inital parameter:
class RecieveLineForm(forms.ModelForm):
purchaseorderline = forms.IntegerField(widget=forms.HiddenInput(), initial=37)
If you need to do it dynamically, then provide it as a dictionary when you create your form in the view:
initial_values = {'purchaseorderline': 37}
form = ReceiverLineForm(initial=initial_values)
Upvotes: 3