Changing request.POST values (QueryDict instance is immutable)

Posted: February 12th, 2010 | Author: Davo | Filed under: Django | Tags: | No Comments »

When trying to change the request.POST values an exception is raised with: QueryDict instance is immutable.
The request.POST is immutable which means you cannot change/alter it.
To cope this you will need to copy() it.

def something(request):
    ...
    if request.method == 'POST':
        post_values = request.POST.copy()
        # post_values can now be changed. For instance if you have a
        post_values['some_input'] = 'I am changing it!'
        # this can be used for forms etc...
        # Ex:
        form = SomeForm(post_values)
    ...