QueryDict instance is immutable

4,028 views
Skip to first unread message

Grupo Django

unread,
Mar 12, 2007, 6:33:26 PM3/12/07
to Django users
Hi!
I have created a form using newforms, and one field is author which I
want to fill using the current username, I did this:
request.POST['author']="Username I want"
and I got this error:
"QueryDict instance is immutable"
Ok, what should I do? I thought about create a hidden widget for this
field, but that's not secure enough.
Is there a way to do what I want?

Thank you!

Mike Axiak

unread,
Mar 12, 2007, 6:42:20 PM3/12/07
to Django users
Hello,

That particular QueryDict instance is immutable, so you won't get what
you want.
Try the following:
new_data = request.POST.copy()
new_data['author'] = "Username I want"

This will create a mutable instance and fill it with the correct data.
You can then use new_data in whatever context you were using
request.POST.

Cheers,
Michael Axiak

limodou

unread,
Mar 12, 2007, 8:50:47 PM3/12/07
to django...@googlegroups.com
You can do like this:

old = request.POST._mutable
request.POST._mutable = True
for k, v in args.items():
request.POST[k] = v
request.POST._mutable = old

But except you have strong idea, I don't suggest that you do like
above. You can copy() it and the new instance can be changed.

--
I like python!
UliPad <<The Python Editor>>: http://wiki.woodpecker.org.cn/moin/UliPad
My Blog: http://www.donews.net/limodou

Reply all
Reply to author
Forward
0 new messages