django.http.QueryDict
objects that are assigned to request.GET
and request.POST
are immutable.
You can convert it to a mutable QueryDict
instance by copying it:
request.GET = request.GET.copy()
Afterwards you’ll be able to modify the QueryDict
:
>>> from django.test.client import RequestFactory
>>> request = RequestFactory().get("https://stackoverflow.com/")
>>> request.GET
<QueryDict: {}>
>>> request.GET['foo'] = 'bar'
AttributeError: This QueryDict instance is immutable
>>> request.GET = request.GET.copy()
<QueryDict: {}>
>>> request.GET['foo'] = 'bar'
>>> request.GET
<QueryDict: {'foo': 'bar'}>
This has been purposefully designed so that none of the application components are allowed to edit the source request data, so even creating a immutable QueryDict
again would break this design. I would still suggest that you follow the guidelines and assign additional request data directly on the request
object in your middleware, despite the fact that it might cause you to edit your sources.