0

考虑以下最简单的示例:

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 响应,我该怎么做?

4

1 回答 1

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
于 2018-02-16T11:44:33.803 回答