I would like to use mutable dict for request.args
and request.form
. Werkzeug and Flask create an ImmutableMultiDict
for this data. Is there a way to make this mutable?
According to the Werkzeug documentation, you update the parameter_storage_class
attribute on the Request
class to use the type you want. If you also want to do the same thing for data other than args
and form
, you update the dict_storage_class
attribute. The documentation notes:
It is ... possible to use mutable structures, but this is not recommended.
It is not recommended to make this mutable because the request data is whatever came from the client. Changing it will mean it no longer matches what the client sent. There is likely a better way to pass around custom values, such as using Flask's before_request
and g
.
The Flask documentation shows how to modify and use a Request
subclass for an application.
from flask import Flask, Request from werkzeug.datastructures import ImmutableOrderedMultiDict class MyRequest(Request): parameter_storage_class = ImmutableOrderedMultiDict class MyFlask(Flask): request_class = MyRequest ```