As explained in Django docs, initial
is not default
.
The initial value of a field is intended to be displayed in an HTML . But if the user delete this value, and finally send back a blank value for this field, the initial
value is lost. So you do not obtain what is expected by a default behaviour.
The default behaviour is : the value that validation process will take if data
argument do not contain any value for the field.
To implement that, a straightforward way is to combine initial
and clean_<field>()
:
class JournalForm(ModelForm):
tank = forms.IntegerField(widget=forms.HiddenInput(), initial=123)
(...)
def clean_tank(self):
if not self['tank'].html_name in self.data:
return self.fields['tank'].initial
return self.cleaned_data['tank']