简体   繁体   English

使用flask_restful 的reqparse,您是否可以忽略params 或json 中未包含的值?

[英]Using flask_restful's reqparse, are you able to ignore values not included in params or json?

Let's say you have a request coming for your API and you're using flask_restful.reqparse to handle the parameters and body of request.假设您有一个针对 API 的请求,并且您正在使用flask_restful.reqparse来处理请求的参数和正文。

POST {{url}}/users
Content-Type: application/json

{
    "firstName": "First",
    "lastName": "Last",
    "age": 25
}
parser = reqparse.RequestParser()
parser.add_argument("firstName", type=str)
parser.add_argument("lastName", type=str)
parser.add_argument("age", type=int)
parser.add_argument("valueInQuestion", type=str)

Is it possible for you to not include values not found in the request when calling .parse_args() ?调用.parse_args()时是否可以不包含请求中未找到的值? For example, in the parser above we have valueInQuestion but the request body doesn't contain this field.例如,在上面的解析器中,我们有valueInQuestion但请求正文不包含此字段。

What I'm getting back from the parser is: {'firstName': 'First', 'lastName': 'Last', 'age': 25, 'valueInQuestion': None} .我从解析器得到的是: {'firstName': 'First', 'lastName': 'Last', 'age': 25, 'valueInQuestion': None}

What I WANT to get back from the parser is: {'firstName': 'First', 'lastName': 'Last', 'age': 25} because valueInQuestion not included.我想从解析器返回的是: {'firstName': 'First', 'lastName': 'Last', 'age': 25}因为valueInQuestion不包括在内。

EDIT: I know I can filter None values from a dict.编辑:我知道我可以从字典中过滤None值。 I'm not looking to do this because if the user hits a POST request with {...valueInQuestion: null} I want to retain that value, not filter it out.我不打算这样做,因为如果用户使用{...valueInQuestion: null}命中 POST 请求,我想保留该值,而不是将其过滤掉。

You can also just filter out items from a dict that has a value with None, just in case If this a option for you您也可以从具有 None 值的 dict 中过滤掉项目,以防万一如果这是您的选项

res = {k:v for k,v in your_dict.items() if v is not None} res = {k:v for k,v in your_dict.items() if v is not None}

There is parameter store_missing for Argument constructor - it is set to True by default. Argument构造函数有参数store_missing - 默认情况下设置为True By setting this argument to False we get only values passed in request and other parser parameters are just skipped, so we get dictionary without None values.通过将此参数设置为False ,我们仅获得请求中传递的值,而其他解析器参数则被跳过,因此我们获得没有None值的字典。 In case anyone is looking for solution, this parameter may help.如果有人正在寻找解决方案,此参数可能会有所帮助。

Source: flask-restful/reqparse.py来源: flask-restful/reqparse.py

Edit:编辑:

reqparse docs say " The whole request parser part of Flask-RESTful is slated for removal and will be replaced by documentation on how to integrate with other packages that do the input/output stuff better (such as marshmallow ). This means that it will be maintained until 2.0 but consider it deprecated. Don't worry, if you have code using that now and wish to continue doing so, it's not going to go away any time too soon. "reqparse 文档说“ Flask-RESTful 的整个请求解析器部分将被删除,并将被有关如何与其他可以更好地执行输入/输出的包(例如marshmallow )集成的文档所取代。这意味着它将是一直保持到 2.0,但认为它已被弃用。别担心,如果您现在有代码使用它并希望继续这样做,它不会很快消失 go。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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