Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I modify the bound value for a field in a bound form in django?

Tags:

django

I override the __init__ method of my Form. I can set the initial value by doing the following:

self.fields['fieldname'].initial = ....

But given that it is bound, calling the above has no effect. I tried doing this:

self.fields['fieldname'].bound_data = ....

but this does not work. Is there a way to do this ?

like image 371
canadadry Avatar asked Nov 23 '11 10:11

canadadry


People also ask

What is form Is_valid () in Django?

The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.

How do you make a field not editable in Django?

disabled. The disabled boolean argument, when set to True , disables a form field using the disabled HTML attribute so that it won't be editable by users. Even if a user tampers with the field's value submitted to the server, it will be ignored in favor of the value from the form's initial data.

What does cleaned_data do in Django?

cleaned_data is where all validated fields are stored.


1 Answers

You can update the form's data dict

self.data['fieldname'] = new_value

bound_data is a method, not an attribute, so you can't set the value there.

request.GET and request.POST are immutable, unless you create a copy(). You could do the copy in your __init__ method, or before you bind the form.

data = request.POST.copy()
form = MyForm(data=data)
like image 136
Alasdair Avatar answered Oct 19 '22 06:10

Alasdair