更改werkzeug请求对象上的值 [英] Changing values on a werkzeug request object
本文介绍了更改werkzeug请求对象上的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个来自werkzeug的请求对象.我想更改此请求对象的值.这是不可能的,因为werkzeug请求对象是不可变的.我了解这个设计决定,但是我需要更改一个值.我该怎么做?
I have a request object that comes from werkzeug. I want to change a value on this request object. This is not possible because werkzeug request objects are immutable. I understand this design decision, but I need to change a value. How do I do this?
>>> request
<Request 'http://localhost:5000/new' [POST]>
>>> request.method
'POST'
>>> request.method = 'GET'
*** AttributeError: read only property
我尝试执行deepcopy
,但是结果副本也是不可变的.我想我可以创建自己的模拟对象并手动填写值,但这是我的最后选择.有更好的方法吗?
I tried doing a deepcopy
, but the resulting copy is immutable also. I guess I could just create my own mock object and fill in the values manually, but that is my last resort solution. Is there a better way?
推荐答案
这是我想出的:
def make_duplicate_request(request):
"""
Since werkzeug request objects are immutable, this is needed to create an
identical request object with mutable values
"""
class Req(object):
method = 'GET'
path = ''
headers = []
args = []
r = Req()
r.path = request.path
r.headers = request.headers
r.is_xhr = request.is_xhr
r.args = request.args
return r
也许不是最优雅的解决方案,但是它可以工作.
Maybe no the most elegant solution, but it works.
这篇关于更改werkzeug请求对象上的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文