Reputation: 5039
I want to change request.GET
querydict object in django. I tried this but changes made by me are not reflected. I tried this
tempdict = self.request.GET.copy() # Empty initially
tempdict['state'] = ['XYZ',]
tempdict['ajaxtype'] = ['facet',]
print self.request.GET
I get
<QueryDict: {}> as my output
Is it possible to change the request.GET
querydict object in django?
Upvotes: 15
Views: 18701
Reputation: 2346
Your code should work if you add one little step: you are now making a copy of the request.GET
, but you have not assigned it back to the request
. So it is just a standalone object which is not related to the request
in any way.
This would the the necessary improvement:
tempdict = self.request.GET.copy()
tempdict['state'] = ['XYZ',]
tempdict['ajaxtype'] = ['facet',]
self.request.GET = tempdict # this is the added line
print(self.request.GET)
I have tested it in writing custom middleware, but I assume it works the same way in all places.
Upvotes: 11
Reputation: 53999
You can't change the request.GET
or request.POST
as they are instances of QueryDict
which are immutable according to the docs:
QueryDict instances are immutable, unless you create a copy() of them. That means you can’t change attributes of request.POST and request.GET directly.
Upvotes: 22