In my application I have a CreateView
that must initialize some fields of the model with a default value, different from the default defined inside the model.
I do not want the user to edit the value, thus I put the field in the exclude
list
class AForm(ModelForm):
class Meta:
model = AModel
exclude = ['a_field']
class AView(CreateView):
form_class = AForm
The question is: where do I set the value of a_field
?
I tried to define clean
methods inside AForm
, like thus
class AForm(ModelForm):
[...]
def clean(self):
d = super(AForm, self).clean()
d['a_field'] = 10
return d
however a_field
is set to whatever its default value defined in the model is, rather than 10
. I also tried defining clean_a_field
, but that is simply not executed.
If I remove a_field
from the exclude
list, then the clean
and clean_a_field
will work, but the form won't validate unless I render some <input name="a_field">
inside the template, which is not optimal.