如何在 Flask restplus Api 中使用基于方法参数的 SELECT 查询?
How to use SELECT query based on method parameter in Flask restplus Api?
我想在我的 Flask REST API
中处理一个 GET
请求。该请求将包含 List
参数,我将其用作典型的 GET 请求:
https://localhost:5000/organisation?List=1
此请求,将return所有Organisation
型号信息。 Organisation
是我的 MySQL 数据库的 table 的模型 class(来自 peewee
)。
我想在请求中输入 PrivenceId
参数,并将 return 所有 Organisation
型号信息,其中 Organisation.PrivencedId == PrivenceId
参数,但我遇到以下错误:
TypeError: 'Organisation' object is not iterable
我的代码(OrganisationController.py
文件)是:
from flask_restplus import Resource, reqparse
from Models.Organisation import Organisation
from flask import request
# Define parser and request args
parser = reqparse.RequestParser()
parser.add_argument('List', type=int)
parser.add_argument('ProvinceId', type=int)
class List(Resource):
def get(self):
args = parser.parse_args()
List = args['List']
ProvinceId = args['ProvinceId']
if List :
Organisations = Organisation.select()
Ls = [dict (
ID = c.ID,
Title = c.Title,
Code = c. Code,
OrgEnumId = c.OrgEnumId,
ProvinceId = c.ProvinceId,
CityId = c.CityId,
Address = c.Address,
Phone = c.Phone,
) for c in Organisations
]
return dict(Organisations = Ls)
elif (ProvinceId) :
Organisations = Organisation.select().where
(
Organisation.ProvinceId ==args['ProvinceId']
).get()
Ls = [dict (
ID = c.ID,
Title = c.Title,
Code = c. Code,
OrgEnumId = c.OrgEnumId,
ProvinceId = c.ProvinceId,
CityId = c.CityId,
Address = c.Address,
Phone = c.Phone,
) for c in Organisations
]
return dict(Organisations = Ls)
和boot.py
运行 api 的文件是:
from flask import Flask
from flask_restplus import Api
from Controllers import OrganisationController
app = Flask(__name__)
api = Api(app)
api.add_resource(OrganisationController.List, '/organisation')
if __name__ == "__main__":
app.run ('127.0.0.1', 5000, True)
你能帮帮我吗?
我更改了这行代码并解决了我的问题:
Organisations = Organisation.select().where
(
Organisation.ProvinceId ==args['ProvinceId']
)
我想在我的 Flask REST API
中处理一个 GET
请求。该请求将包含 List
参数,我将其用作典型的 GET 请求:
https://localhost:5000/organisation?List=1
此请求,将return所有Organisation
型号信息。 Organisation
是我的 MySQL 数据库的 table 的模型 class(来自 peewee
)。
我想在请求中输入 PrivenceId
参数,并将 return 所有 Organisation
型号信息,其中 Organisation.PrivencedId == PrivenceId
参数,但我遇到以下错误:
TypeError: 'Organisation' object is not iterable
我的代码(OrganisationController.py
文件)是:
from flask_restplus import Resource, reqparse
from Models.Organisation import Organisation
from flask import request
# Define parser and request args
parser = reqparse.RequestParser()
parser.add_argument('List', type=int)
parser.add_argument('ProvinceId', type=int)
class List(Resource):
def get(self):
args = parser.parse_args()
List = args['List']
ProvinceId = args['ProvinceId']
if List :
Organisations = Organisation.select()
Ls = [dict (
ID = c.ID,
Title = c.Title,
Code = c. Code,
OrgEnumId = c.OrgEnumId,
ProvinceId = c.ProvinceId,
CityId = c.CityId,
Address = c.Address,
Phone = c.Phone,
) for c in Organisations
]
return dict(Organisations = Ls)
elif (ProvinceId) :
Organisations = Organisation.select().where
(
Organisation.ProvinceId ==args['ProvinceId']
).get()
Ls = [dict (
ID = c.ID,
Title = c.Title,
Code = c. Code,
OrgEnumId = c.OrgEnumId,
ProvinceId = c.ProvinceId,
CityId = c.CityId,
Address = c.Address,
Phone = c.Phone,
) for c in Organisations
]
return dict(Organisations = Ls)
和boot.py
运行 api 的文件是:
from flask import Flask
from flask_restplus import Api
from Controllers import OrganisationController
app = Flask(__name__)
api = Api(app)
api.add_resource(OrganisationController.List, '/organisation')
if __name__ == "__main__":
app.run ('127.0.0.1', 5000, True)
你能帮帮我吗?
我更改了这行代码并解决了我的问题:
Organisations = Organisation.select().where
(
Organisation.ProvinceId ==args['ProvinceId']
)