如何在Flask-RESTless中使用关键字参数 [英] How to use the keyword arguments in Flask-RESTless

查看:1357
本文介绍了如何在Flask-RESTless中使用关键字参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


预处理程序和后处理程序的参数函数将作为关键字参数提供,因此在定义预处理器或后处理函数时,应始终将** kw作为最终参数。

但是它没有指定我们如何使用这些关键字参数将信息传递给前处理程序或后处理程序。任何人都可以告诉我们如何做到这一点?

我们的create_api看起来像这样:

 

create_api(富,
方法= [ 'GET', 'POST', 'PUT', '删除'],
集合名称= 'p',
url_prefix = '/ API / V1',
primary_key = 'UID',
exclude_columns = [ 'ID'],
的预处理器= {
'POST':[认证,validation_preprocessor]
'GET_SINGLE':[验证],
'GET_MANY':[验证],
'PUT_SINGLE':[认证,validation_preprocessor],
'PUT_MANY':[认证,validation_preprocessor ],
'DELETE':[authenticate]
})

def validation_preprocessor(data = None,** kw):
#做东西
通过

我们要做的是在validation_preprocessor中使用** kw来为我们的ow n值。

解决方案

通过阅读文档,您不会将数据传递到预处理器,您预处理器和数据传递给

数据的确切格式取决于具体的方法:

https://flask-restless.readthedocs.org/en/latest/customizing。 html#request-preprocessors-and-postprocessors


每种类型的请求的预处理器和后处理器接受不同的参数。大多数应该没有返回值(更具体地说,任何返回的值被忽略)....那些接受字典作为参数的预处理器和后处理器可以(而且应该)就地修改它们的参数。

你不直接使用* kw,它只是为了让你的代码向前兼容Flask-RESTLess,所以如果他们决定更新API并发送在你的特定的例子中,你只需编辑 data dictionary,因为Python变量是通过赋值 ,一旦你编辑它,它被编辑为链的其余部分。

$ p $ def $ validation $ preprocessor(data = None,** kw) :
if data:
data [foobar] =rarr我是恐龙

我个人认为这是混乱而不是我如何期望的事情工作,但我认为他们有一个原因。


Me and a buddy have been reading through the docs for Flask-RESTless and it says:

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.

but it doesn't specify how we can use these keyword argument to pass info to the pre- or postprocessor. Can anyone tell us how to do this?

Our create_api looks like this right now:

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

What we want to do is use **kw in validation_preprocessor for our own values.

解决方案

From reading the docs, you don't pass data to the pre-processor, you are the pre-processor and data is passed to you.

The exact format of the data depends on the specific method:

https://flask-restless.readthedocs.org/en/latest/customizing.html#request-preprocessors-and-postprocessors

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.

You don't use the *kw directly, it's just there to make your code forwards compatible with Flask-RESTLess so if they decided to update the API and send a different set of parameters to your function, it won't break.

In your particular example, you would just edit the data dictionary and since Python variables are pass by assignment, once you edit it, it's edited for the rest of the chain.

def validation_preprocessor(data=None, **kw):
    if data:
        data["foobar"] = "rarr I'm a dinosaur"

I personally think that's confusing and not how I would expect things to work, but I assume they had a reason for it.

这篇关于如何在Flask-RESTless中使用关键字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆