0

我正在尝试使用 flask-mongoAlchemy 对以下模式进行发布请求

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')

在模式网关类包含其他三个类的列表

尝试如下所示

@gateway_bp.route('/gateway',methods=['POST'])
def object_book():
    request_data = request.get_json()

    sensorsList=[]
    cameraList=[]
    checkList=[]

    for sensorObj in request_data['GateWay1']['sensors']:
        sensorsList.append(Sensors(sensorName=sensorObj['sensorName'],sensorStatus=sensorObj['sensorStatus']))
        gatewayObj=Gateway(sensors=sensorsList)

    for cameraObj in request_data['GateWay1']['camera']:
        cameraList.append(Camera(cameraName=cameraObj['cameraName'],cameraStatus=cameraObj['cameraStatus']))
        gatewayObj=Gateway(camera=cameraList)


    for checkListObj in request_data['GateWay1']['checkpoints']:
        checkList.append(Checkpoints(checkPointName=checkListObj['checkPointName']))
        gatewayObj=Gateway(checkpoints=checkList)

        gatewayObj.save()
        return 'Saved :)'

得到错误有

mongoalchemy.exceptions.MissingValueException

MissingValueException: sensors

样本有效载荷

{

    "GateWay1": {
        "sensors": [{
            "sensorName": "s1",
            "sensorStatus": "green "
        }, {
            "sensorName": "s2",
            "sensorStatus": "red "
        }],
        "camera": [{
            "cameraName": "cam1",
            "cameraStatus": "live"
        }, {
            "cameraName": "cam2",
            "cameraStatus": "live"
        }],
        "checkpoints": [{
            "checkPointName": "chk1"
        }, {
            "checkPointName": "chk2"
        }]


    }


}

请帮助使用 mongoAlchemy 将上述 json 保存在 mongodb 中,请指出一些示例 repo,我可以在其中了解有关 flask-mongoAlchemy 的更多信息

4

0 回答 0