使用python flask列出对象数组

时间:2017-01-02 07:32:04

标签: python mongodb flask mongodb-query mongoalchemy

我有一个如下所示的有效载荷,存储在mongodb

{ 
    "_id" : ObjectId("5865fbf5558b670ac091c81e"), 
    "sensors" : [
        {
            "sensorStatus" : "green ", 
            "sensorName" : "s1"
        }, 
        {
            "sensorStatus" : "red ", 
            "sensorName" : "s2"
        }
    ], 
    "camera" : [
        {
            "cameraName" : "cam1", 
            "cameraStatus" : "live"
        }, 
        {
            "cameraName" : "cam2", 
            "cameraStatus" : "live"
        }
    ], 
    "checkpoints" : [
        {
            "checkPointName" : "chk1"
        }
    ]
}

尝试写路由以显示此数据,如下所示

@gateway_bp.route('/gateway',methods=['GET'])
def list_gateWay():
    gateways = Gateway.query.all()
    print gateways
    return Response(json.dumps(gateways),  mimetype='application/json')

我收到错误

TypeError: <app.model.canvasmodel.Gateway object at 0x02E99D10> is not JSON serializable

架构如下所示

from app.dbconfig.extensions import db

class Sensors(db.Document):
    sensorName = db.StringField()
    sensorStatus = db.StringField()


class Camera(db.Document):
    cameraName = db.StringField()
    cameraStatus = db.StringField()

class Checkpoints(db.Document):
    checkPointName = db.StringField()

class Gateway(db.Document):
    sensors = db.ListField(db.DocumentField(Sensors), db_field='sensors')
    camera = db.ListField(db.DocumentField(Camera), db_field='camera')
    checkpoints = db.ListField(db.DocumentField(Checkpoints), db_field='checkpoints')

请帮助纠正获取请求,谢谢

0 个答案:

没有答案