如何记录Flask-Restplus发送的封送消息

时间:2018-02-16 11:36:49

标签: python flask flask-sqlalchemy flask-restplus

请考虑以下最基本的例子:

from flask_restplus import Api, Resource, fields
from .models import User

ns = api.namespace('users', description='User related operations')

user = api.model('User', {
    'id': fields.Integer(readOnly=True, description='The user identifier'),
    'name': fields.String(required=True, description='The users name'),
    'password': fields.String(required=True, description='The users password')
})

@ns.route('/')
class UserList(Resource):
    @ns.doc('list_users')
    @ns.marshal_list_with(user)
    def get(self):
        data = User.query.all()
        print "Print the response here!"
        return data

我正在排除故障,我希望能够打印/记录Flask-RestPlus返回的JSON响应,我该怎么做?

1 个答案:

答案 0 :(得分:0)

突然间,我很快就弄明白了。使用marshal函数与@ ns.marshal_list_with(user)注释相同

from flask_restplus import Api, Resource, fields, marshal
from .models import User
import json

ns = api.namespace('users', description='User related operations')

user = api.model('User', {
    'id': fields.Integer(readOnly=True, description='The user identifier'),
    'name': fields.String(required=True, description='The users firstName'),
    'password': fields.String(required=True, description='The users password')
})

@ns.route('/')
class UserList(Resource):
    @ns.doc('list_users')
    @ns.marshal_list_with(user)
    def get(self):
        data = User.query.all()
        print json.dumps(marshal(User.query.all(), user))
        return data