如何使用patch_internal
How to use patch_internal
在我的应用程序中,我有一个 Hook 应该更新资源中的字段:
另一个post有人用了patch_internal方法,但是我没看懂,怎么弄的。
from my_application import app
from eve.methods.patch import patch_internal
def my_hook(...):
with app.test_request_context():
patch_internal("my_resource", payload={"bar": "bar_value"}, lookup={"foo": "foo_value"})
我尝试在 settings.py
中使用 PRESERVE_CONTEXT_ON_EXCEPTION = False
。
根据我尝试的方式,我得到其中之一
原始请求的 404,首先触发了挂钩,
或断言错误:
.
Debugging middleware caught exception in streamed response at a point where response headers were already sent.
Traceback (most recent call last):
File ".../lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File ".../lib/python2.7/site-packages/flask/app.py", line 1825, in wsgi_app
ctx.auto_pop(error)
File ".../lib/python2.7/site-packages/flask/ctx.py", line 374, in auto_pop
self.pop(exc)
File ".../lib/python2.7/site-packages/flask/ctx.py", line 357, in pop
% (rv, self)
AssertionError: Popped wrong request context.
(<RequestContext 'http://127.0.0.1:5001/' [GET] of eve> instead of <RequestContext 'http://127.0.0.1:5001/my_endpoint' [GET] of eve>)
我的问题是:
patch_internal 的正确参数是什么?
我要如何告诉 Eve,我要更改哪个项目?
这是一个应该有效的简单示例:
from eve import Eve
from eve.methods.patch import patch_internal
app = Eve()
def my_hook(*args):
with app.test_request_context():
payload = {"bar": "bar_value"}
lookup = {"_id": "4f71e038c88e201118000002"}
patch_internal("my_resource", payload, **lookup)
# this is rather stupid. We're going to patch the same document on *every* GET request, but you get the point
app.on_post_GET += my_hook
if __name__ == '__main__':
app.run()
或者您可以查看 patch internal test for a slightly more articulate example. Also here is a good post_internal example。希望这有帮助。
在我的应用程序中,我有一个 Hook 应该更新资源中的字段: 另一个post有人用了patch_internal方法,但是我没看懂,怎么弄的。
from my_application import app
from eve.methods.patch import patch_internal
def my_hook(...):
with app.test_request_context():
patch_internal("my_resource", payload={"bar": "bar_value"}, lookup={"foo": "foo_value"})
我尝试在 settings.py
中使用 PRESERVE_CONTEXT_ON_EXCEPTION = False
。
根据我尝试的方式,我得到其中之一
原始请求的 404,首先触发了挂钩,
或断言错误:
.
Debugging middleware caught exception in streamed response at a point where response headers were already sent.
Traceback (most recent call last):
File ".../lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File ".../lib/python2.7/site-packages/flask/app.py", line 1825, in wsgi_app
ctx.auto_pop(error)
File ".../lib/python2.7/site-packages/flask/ctx.py", line 374, in auto_pop
self.pop(exc)
File ".../lib/python2.7/site-packages/flask/ctx.py", line 357, in pop
% (rv, self)
AssertionError: Popped wrong request context.
(<RequestContext 'http://127.0.0.1:5001/' [GET] of eve> instead of <RequestContext 'http://127.0.0.1:5001/my_endpoint' [GET] of eve>)
我的问题是:
patch_internal 的正确参数是什么? 我要如何告诉 Eve,我要更改哪个项目?
这是一个应该有效的简单示例:
from eve import Eve
from eve.methods.patch import patch_internal
app = Eve()
def my_hook(*args):
with app.test_request_context():
payload = {"bar": "bar_value"}
lookup = {"_id": "4f71e038c88e201118000002"}
patch_internal("my_resource", payload, **lookup)
# this is rather stupid. We're going to patch the same document on *every* GET request, but you get the point
app.on_post_GET += my_hook
if __name__ == '__main__':
app.run()
或者您可以查看 patch internal test for a slightly more articulate example. Also here is a good post_internal example。希望这有帮助。