Reputation: 1981
I want to reset the value of some fields in my django form to None
, inside the __init__
method of my form.
This is what I have so far:
class MyFormForm(forms.ModelForm):
class Meta:
model = MyModel
fields = ['field1', ...]
field1 = forms.IntegerField(max_value=100)
def __init__(self, *args, **kwargs):
values_changed = kwargs.pop('values_changed', False)
super(MyFormForm, self).__init__(*args, **kwargs)
self.data = self.data.copy()
if not values_changed:
for field in self.fields:
self.data[field] = None
Unfortunately, when the form is displayed in my template, the value that has been POSTed is still in it. How do I get rid of the value, so that the change takes effect in the template, which renders the form?
Things that don't work:
cleaned_data
does not existI'm accessing the values like this:
{{ form.field1.value|default_if_none:"Please enter." }}
edit: I just tried
{{ form.data.field1|default_if_none:"Please enter." }}
no change.
Upvotes: 0
Views: 1383
Reputation: 1981
I'm a moron. There were two forms, form
and form_2
.
I used the wrong variable name. My bad. It works like it should with the code above.
Upvotes: 1