How can I change a Django form field value before saving?


if request.method == 'POST':
    userf = UsersModelForm(request.POST)
    username =['username']
    password =['password']
    passwordrepeat =['passwordrepeat']
    email =['email']

I tried this:

    tempSalt = bcrypt.gensalt()
    password = bcrypt.hashpw(password,tempSalt)
    passwordrepeat = bcrypt.hashpw(passwordrepeat,tempSalt)['password'] = password['passwordrepeat'] = passwordrepeat

But i got error. How can i change the value of['password'] and['passwordrepeat'] before saving?


AttributeError at /register

This QueryDict instance is immutable

Request Method:     POST
Request URL:
Django Version:     1.3.1
Exception Type:     AttributeError
Exception Value:    

This QueryDict instance is immutable

Exception Location:     /usr/local/lib/python2.6/dist-packages/django/http/ in _assert_mutable, line 359
Python Executable:  /usr/bin/python
Python Version:     2.6.6
Python Path:    

Asked By: shibly



Override _clean methods and put your checks in them. You can modify cleaned_data from there.


def clean_password(self):
    new1 = self.cleaned_data['password']
    return new1

Every fields in the form will have a field_name_clean() method created automatically by Django. This method is called when you do form.is_valid().

Answered By: e-satis

See the documentation for the save() method

if request.method == 'POST':
    userf = UsersModelForm(request.POST)
    new_user =

    username = userf.cleaned_data['username']
    password = userf.cleaned_data['password']
    passwordrepeat = userf.cleaned_data['passwordrepeat']
    email = userf.cleaned_data['email']

    new_user.password = new1
    new_user.passwordrepeat = new2
Answered By: Burhan Khalid

If you need to do something to the data before saving, just create a function like:

def clean_nameofdata(self):
    data = self.cleaned_data['nameofdata']
    # do some stuff
    return data

All you need is to create a function with the name **clean_***nameofdata* where nameofdata is the name of the field, so if you want to modify password field, you need:

def clean_password(self):

if you need to modify passwordrepeat

def clean_passwordrepeat(self):

So inside there, just encrypt your password and return the encrypted one.

I mean:

def clean_password(self):
    data = self.cleaned_data['password']
    # encrypt stuff
    return data

so when you valid the form, the password would be encrypted.

Answered By: Jesus Rodriguez

You will have problems if you need to fill form from POST, change any form field value and render form again.
Here is solution for it:

class StudentSignUpForm(forms.Form):
  step = forms.IntegerField()

  def set_step(self, step):
    data =
    data['step'] = step = data

And then:

form = StudentSignUpForm(request.POST)
if form.is_valid() and something():
  return  render_to_string('form.html', {'form': form})
Answered By: Serafim Suhenky

In Views:

def post(self, request, *args, **kwargs):
    form = self.form_class(request.POST)
    if form.is_valid():
        obj =
        obj.my_field = 'value'

In Forms:

instance = form.instance
instance.user = request.user

But be careful, this does not check is_valid(). If you want to do that, you can instantiate the form with the new values:

form = MyForm(instance=instance)
if form.is_valid():
Answered By: Özer
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.