Python Flask RESTPlus:如何在嵌套API模型中获得查询sqlite数据库的结果?
我正在用Flask RESTPlus编写一个api,并希望使用sqlite数据库 首先,我的当前代码: API模型Python Flask RESTPlus:如何在嵌套API模型中获得查询sqlite数据库的结果?,python,sql,flask,sqlalchemy,flask-restplus,Python,Sql,Flask,Sqlalchemy,Flask Restplus,我正在用Flask RESTPlus编写一个api,并希望使用sqlite数据库 首先,我的当前代码: API模型 lightstate = api.model('LightState', { 'red': fields.Integer(description='red part of the rgb light'), 'green': fields.Integer(description='green part of the rgb light'), 'blue': f
lightstate = api.model('LightState', {
'red': fields.Integer(description='red part of the rgb light'),
'green': fields.Integer(description='green part of the rgb light'),
'blue': fields.Integer(description='blue part of the rgb light'),
'bri': fields.Integer(description='brightness of the light')
})
lights = api.model('Lights', {
'id': fields.Integer(readOnly=True, description='The database id of the light'),
'name': fields.String(required=True, description='light name'),
'state': fields.Nested(lightstate)
})
数据库模型
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Lights(db.Model):
__bind_key__ = 'lights'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255))
red = db.Column(db.Integer)
green = db.Column(db.Integer)
blue = db.Column(db.Integer)
bri = db.Column(db.Integer)
def __init__(self, name, red, green, blue, bri):
self.name = name
self.red = red
self.green = green
self.blue = blue
self.bri = bri
lights = api.model('Lights', {
'id': fields.Integer(readOnly=True, description='The database id of the light'),
'name': fields.String(required=True, description='light name'),
'red': fields.Integer(description='red part of the rgb light'),
'green': fields.Integer(description='green part of the rgb light'),
'blue': fields.Integer(description='blue part of the rgb light'),
'bri': fields.Integer(description='brightness of the light')
})
创建数据库条目
def create_light(data):
name = data.get('name')
state = data.get('state')
red = state['red']
green = state['green']
blue = state['blue']
bri = state['bri']
entry = Lights(name, red, green, blue, bri)
db.add(entry)
请求处理程序
@namespace.route('/')
class Light(Resource):
@api.marshal_with(lights)
def get(self):
lights_data = Lights.query.all()
return lights_data
@api.expect(lights)
def post(self):
create_light(request.json)
return None, 200
如您所见,我想在数据库中存储名称、rbg值和灯光亮度。使用post请求编写数据库效果很好。
但是get请求的结果如下所示:
[
{
"id": 1,
"name": "garden",
"state": {
"red": null,
"green": null,
"blue": null,
"bri": null
}
},
{
"id": 2,
"name": "pool",
"state": {
"red": null,
"green": null,
"blue": null,
"bri": null
}
}
]
如您所见,灯光值为空。我知道,我的问题与API模型定义中的嵌套字段有关,因为当我将所有部分放在一个模型中时,它就起作用了
更改了API型号
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Lights(db.Model):
__bind_key__ = 'lights'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255))
red = db.Column(db.Integer)
green = db.Column(db.Integer)
blue = db.Column(db.Integer)
bri = db.Column(db.Integer)
def __init__(self, name, red, green, blue, bri):
self.name = name
self.red = red
self.green = green
self.blue = blue
self.bri = bri
lights = api.model('Lights', {
'id': fields.Integer(readOnly=True, description='The database id of the light'),
'name': fields.String(required=True, description='light name'),
'red': fields.Integer(description='red part of the rgb light'),
'green': fields.Integer(description='green part of the rgb light'),
'blue': fields.Integer(description='blue part of the rgb light'),
'bri': fields.Integer(description='brightness of the light')
})
输出获取请求
[
{
"id": 1,
"name": "garden",
"red": 50,
"green": 205,
"blue": 50,
"bri": 84
},
{
"id": 2,
"name": "pool",
"red": 255,
"green": 0,
"blue": 255,
"bri": 96
}
]
如何在“状态”键中从数据库中获取值?您是否检查了数据库中是否设置了这些值?换句话说,当您提交post请求后运行
SELECT*FROM Lights
时,您的DB会返回什么?这些值都在DB中设置。请参阅我最后一篇“代码”帖子。这不是问题。您是否检查了数据库中是否设置了这些值?换句话说,当您提交post请求后运行SELECT*FROM Lights
时,您的DB会返回什么?这些值都在DB中设置。请参阅我最后一篇“代码”帖子。这不是问题。