【发布时间】:2021-07-24 23:58:13
【问题描述】:
我有以下代码:
class CabinetList(Resource):
def get(self):
devices = Device.query.filter(Device.type == 'CABINET').all()
return {'cabinets':list(x.json() for x in devices)}
生成这个存储在 JSON MySql 列中的 JSON:
{
"cabinets":[
{
"id":4,
"name":"Armario 1",
"online":true,
"setup":0.0,
"type":"CABINET",
"data":{
"lockers":[
{
"id":1,
"content":"Pala",
"enabled":true,
"busy":false
},
{
"id":2,
"content":"Azada",
"enabled":true,
"busy":false
}
]
}
}
]
}
使用此代码,我可以更改所选索引的“忙碌”属性
def get(self, device_id, locker_id):
if not 1 <= locker_id <= 32:
return {'error': 'device not found'}, 404
device = Device.query.filter(and_(Device.id == device_id, Device.type == 'CABINET')).first()
if not device:
return {'error': 'device not found'}, 404
# Update the current status for the locker
device.data['lockers'][0]['busy'] = True
return {'cabinet': device.json()}
它有效,但我不想通过索引来引用该项目我想更改匹配其“id”的项目的属性
【问题讨论】:
标签: python mysql json sqlalchemy flask-sqlalchemy