简体   繁体   中英

How can I use mutable dict for Werkzeug's Request data?

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 ```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM