Making a Django POST mutable

For good reasons, the Django Request object’s POST attribute is immutable. No one wants anyone mucking around with important data there. But I had a case where I wanted to re-use some view code and it would involve using a POST object again, but with a few added parameters.

You can copy the POST data into a QueryDict, but I found that if you want to forward the actual request, you have to copy.copy() the request using the python standard copy library. Maybe this is obvious but it might save someone(like me) an hour of searching and trial/error.

def my_view(request)
    req_copy = copy.copy(request)
    req_copy.POST = request.POST.copy()
    req_copy.POST.update({'added_param':'my_value'})
    my_DRY_view(req_copy)

def my_DRY_view(req_copy)
        cool_stuff()

Tags: , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: