Flask Rest 默认参数
Flask Restul default parameter
我正在尝试为我的烧瓶指定一个可选参数 restful api。
我定义了以下路线,其中模式是可选的。
我的路由是这样定义的
api.add_resource(TestController, "/test/<id>/<mode>")
我的控制器定义如下:
class TestController(Resource):
def get(
self,
id: int,
mode: int = -1,
):
我读到一种方法是这样
api.add_resource(TestController, "/test/<id>/")
api.add_resource(TestController, "/test/<id>/<mode>")
但这会导致我出错并给我:
AssertionError: View function mapping is overwriting an existing endpoint function: loggers
这里有几点我要修改。
从第一个声明中删除尾随 /:
api.add_resource(TestController, "/test/")
将预期的类型添加到获取参数中,例如:
api.add_resource(TestController, "/test/int:id")
api.add_resource(TestController, "/test/int:id/int:模式")
我不会将两条不同的路线与相同的路线联系起来 class。
class TestController1(资源):
定义得到(
自己,
编号:整数,
):
class TestController2(资源):
定义得到(
自己,
编号:整数,
模式:int = -1
):
然后相应地改变你的路线。
如果一定要这样做,那么路由最好这样写:
api.add_resource(TestController, "/test/<int:id>", "/test/<int:id>/<int:mode>")
This Whosebug post 可能有用
我正在尝试为我的烧瓶指定一个可选参数 restful api。
我定义了以下路线,其中模式是可选的。
我的路由是这样定义的
api.add_resource(TestController, "/test/<id>/<mode>")
我的控制器定义如下:
class TestController(Resource):
def get(
self,
id: int,
mode: int = -1,
):
我读到一种方法是这样
api.add_resource(TestController, "/test/<id>/")
api.add_resource(TestController, "/test/<id>/<mode>")
但这会导致我出错并给我:
AssertionError: View function mapping is overwriting an existing endpoint function: loggers
这里有几点我要修改。
从第一个声明中删除尾随 /:
api.add_resource(TestController, "/test/")
将预期的类型添加到获取参数中,例如:
api.add_resource(TestController, "/test/int:id") api.add_resource(TestController, "/test/int:id/int:模式")
我不会将两条不同的路线与相同的路线联系起来 class。
class TestController1(资源): 定义得到( 自己, 编号:整数, ):
class TestController2(资源): 定义得到( 自己, 编号:整数, 模式:int = -1 ):
然后相应地改变你的路线。
如果一定要这样做,那么路由最好这样写:
api.add_resource(TestController, "/test/<int:id>", "/test/<int:id>/<int:mode>")
This Whosebug post 可能有用