简体   繁体   English

Python Flask_restplus flash_restx 动态编组响应

[英]Python Flask_restplus flash_restx dynamic marshalling response

Is it possible to dynamicaly modify the marshalled response model (ie: change fields list, add mask, ...)?是否可以动态修改编组响应 model(即:更改字段列表,添加掩码,...)?

ex:前任:

from flask_restplus import Resource, fields

model = api.model('Model', {
    'name': fields.String,
    'address': fields.String,
    'date_updated': fields.DateTime(dt_format='rfc822'),
})

@api.route('/todo')
class Todo(Resource):
    @api.marshal_with(model, envelope='resource')
    def get(self, **kwargs):
        return db_get_todo()  # Some function that queries the db

Here the marshalling is staticaly declared with decorator.这里的编组是用装饰器静态声明的。 If i would like to mask from example date_updated when user is not admin, or depending on user preferences I can't.如果我想在用户不是管理员时屏蔽示例 date_updated,或者根据用户偏好我不能。

I saw this example: https://blog.fossasia.org/dynamically-marshaling-output-in-flask-restplus/ It is interesting but it uses another static model, so it is not trully dynamic and implies code duplication (sure I can use inherit,...)我看到了这个例子: https://blog.fossasia.org/dynamically-marshaling-output-in-flask-restplus/这很有趣,但它使用了另一个 static model,所以它不是真正的动态并且意味着代码重复(当然我可以使用继承,...)

What I would like is be able to change dynamicaly the fields or add a mask from a list that could come from a db for example (user preferences or rights).我想要的是能够动态更改字段或从可能来自数据库的列表中添加掩码(例如用户首选项或权限)。

I have tried to manualy marshal the answer我试图手动整理答案

wanted_field_list='name,address'
return  marshal(db_get_todo(),model , mask=wanted_field_list),  200

If I remove the decorator @marshall_with it works perfectly but the drawback is I don't have Swagger doc anymore如果我删除装饰器@marshall_with,它会完美地工作,但缺点是我不再有 Swagger 文档

{ 'name':'blabla',
'address':'xxx'}

If I keep the decorator it still works by the unwanted fields are still rendered with a Null value:如果我保留装饰器,它仍然可以工作,因为不需要的字段仍然使用 Null 值呈现:

{ 'name':'blabla',
'address':'xxx',
'date_updated : null}

This is not the expected result这不是预期的结果

I tried to move to flask_restx and my swagger is not rendered at all and I have some other problems.我试图移动到 flask_restx,但我的 swagger 根本没有呈现,我还有一些其他问题。

Any help is very welcome !非常欢迎任何帮助!

I know its a bit late, but here it is anyway incase someone needs it:我知道它有点晚了,但无论如何,如果有人需要它:

You have a few options to accomplish what you need:您有几个选项可以完成您的需求:

  • Use the skip_none flag to true .使用skip_none 标志为 true This would ignore the date_updated field from the response when it is null.当它为空时,这将忽略响应中的date_updated字段。
  • Use the X-Fields mask when calling the API.调用 API 时使用X-Fields 掩码 The mask value is a list of comma separated variable that you'd like to fetch.掩码值是您要获取的逗号分隔变量列表。 Rest are ignored.休息被忽略。
  • The one in the blog post you linked, which you didn't like... ;)您链接的博客文章中的那个,您不喜欢...;)

use the following decorator使用以下装饰器

@api.marshal_with(model, skip_none=True)

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

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