marshal 工具 方式有2中
步骤:
使用marshal_with 装饰器
先自定义想要返回的的数据格式
class User(object):
def init(self, user_id, name, age):
self.user_id = user_id
self.name = name
self.age = age
my_fields = {user_id’: fields.Integer, ‘naem’:fields.sting
@marshal_with (my_fields)
def get(self):
user = User(1,‘laoshi’,19)
return user
使用marshal
user_dict = marshal(user, self.my_fields, envelope=‘data’)
return user_dict
envelope 是为了可以{“data”: {“user_id”: 1, “name”: “laoshi”}}
可以格式化我们的字典格式
在视图类中可以使用marshall 返回我们需要的字典格式
Flask-RESTful风格marshal 工具
最新推荐文章于 2023-12-02 17:25:27 发布