【发布时间】:2015-02-17 23:39:53
【问题描述】:
我一直在为我的服务器使用 Flask-Classy,它运行良好。但是,我遇到了一个我没有见过的用例,但这是一个非常常见的用例,所以如果不可能的话,我会感到震惊。
我有两个要嵌套的 API,我的意思是我有:
class UsersView(FlaskView):
decorators = [jwt_required()]
route_prefix = '/api/v1/'
def index(self):
...
位于http://example.com/api/v1/users,我可以通过http://example.com/api/v1/users/1访问用户1
现在,我将如何编写 FlaskView 让我做这样的事情? http://example.com/api/v1/users/1/devices/3
当我尝试在 route_prefix 中嵌入资源 id 时,我得到一个关键字参数错误:
class DevicesView(FlaskView):
decorators = [jwt_required()]
route_prefix = '/api/v1/users/<user_id>/'
def index(self):
...
TypeError: index() 得到了一个意外的关键字参数 'user_id'
最后一点是,我自然可以使用 kwargs:
route_prefix = '/api/v1/users/<user_id>/'
def test(self, **kwargs):
print kwargs['user_id']
http://example.com/api/v1/users/103/devices 会吐出 '103',但是,使用 kwargs 感觉有点做作。有没有更好的办法?
【问题讨论】:
标签: python rest flask crud flask-restful