如何在 Flask-RESTless 中使用关键字参数
How to use the keyword arguments in Flask-RESTless
我和一个伙伴一直在阅读 Flask-RESTless 的文档,它说:
The arguments to the preprocessor and postprocessor functions will be provided as keyword arguments, so you should always add **kw as the final argument when defining a preprocessor or postprocessor function.
但它没有指定我们如何使用这些关键字参数将信息传递给预处理器或后处理器。谁能告诉我们该怎么做?
我们的 create_api 现在看起来像这样:
create_api(Foo,
methods=['GET', 'POST', 'PUT', 'DELETE'],
collection_name='p',
url_prefix='/api/v1',
primary_key='uid',
exclude_columns=['id'],
preprocessors={
'POST': [authenticate, validation_preprocessor],
'GET_SINGLE': [authenticate],
'GET_MANY': [authenticate],
'PUT_SINGLE': [authenticate, validation_preprocessor],
'PUT_MANY': [authenticate, validation_preprocessor],
'DELETE': [authenticate]
})
def validation_preprocessor(data=None, **kw):
# Do stuff
pass
我们想要做的是在 validation_preprocessor 中使用 **kw 作为我们自己的值。
通过阅读文档,您不会将数据传递给预处理器,您是预处理器,数据传递给您。
数据的具体格式取决于具体方法:
The preprocessors and postprocessors for each type of request accept different arguments. Most of them should have no return value (more specifically, any returned value is ignored)....Those preprocessors and postprocessors that accept dictionaries as parameters can (and should) modify their arguments in-place.
您不直接使用 *kw,它只是为了让您的代码与 Flask-RESTLess 兼容,所以如果他们决定更新 API 并向您的函数发送一组不同的参数,不会坏的。
在您的特定示例中,您只需编辑 data
字典,因为 Python 变量是 pass by assignment,一旦您编辑它,它就会为链的其余部分编辑。
def validation_preprocessor(data=None, **kw):
if data:
data["foobar"] = "rarr I'm a dinosaur"
我个人认为这令人困惑,而不是我期望事情如何运作,但我认为他们这样做是有原因的。
我和一个伙伴一直在阅读 Flask-RESTless 的文档,它说:
The arguments to the preprocessor and postprocessor functions will be provided as keyword arguments, so you should always add **kw as the final argument when defining a preprocessor or postprocessor function.
但它没有指定我们如何使用这些关键字参数将信息传递给预处理器或后处理器。谁能告诉我们该怎么做?
我们的 create_api 现在看起来像这样:
create_api(Foo,
methods=['GET', 'POST', 'PUT', 'DELETE'],
collection_name='p',
url_prefix='/api/v1',
primary_key='uid',
exclude_columns=['id'],
preprocessors={
'POST': [authenticate, validation_preprocessor],
'GET_SINGLE': [authenticate],
'GET_MANY': [authenticate],
'PUT_SINGLE': [authenticate, validation_preprocessor],
'PUT_MANY': [authenticate, validation_preprocessor],
'DELETE': [authenticate]
})
def validation_preprocessor(data=None, **kw):
# Do stuff
pass
我们想要做的是在 validation_preprocessor 中使用 **kw 作为我们自己的值。
通过阅读文档,您不会将数据传递给预处理器,您是预处理器,数据传递给您。
数据的具体格式取决于具体方法:
The preprocessors and postprocessors for each type of request accept different arguments. Most of them should have no return value (more specifically, any returned value is ignored)....Those preprocessors and postprocessors that accept dictionaries as parameters can (and should) modify their arguments in-place.
您不直接使用 *kw,它只是为了让您的代码与 Flask-RESTLess 兼容,所以如果他们决定更新 API 并向您的函数发送一组不同的参数,不会坏的。
在您的特定示例中,您只需编辑 data
字典,因为 Python 变量是 pass by assignment,一旦您编辑它,它就会为链的其余部分编辑。
def validation_preprocessor(data=None, **kw):
if data:
data["foobar"] = "rarr I'm a dinosaur"
我个人认为这令人困惑,而不是我期望事情如何运作,但我认为他们这样做是有原因的。