Flask-Restful错误:"as_view"方法未继承 [英] Flask-Restful error: "as_view" method not inherited

查看:284
本文介绍了Flask-Restful错误:"as_view"方法未继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flask框架和Flask-RESTful插件编写RESTful API.我在此插件提供的Resource类之上定义了API类,如

I am writting a RESTful API using the Flask framework and the Flask-RESTful plugin. I define my API classes on top of the Resource class that this plugin provides, as in the examples. However, when I want to register my classes using the add_resource method I get the following error:

AttributeError: type object 'UserAPI' has no attribute 'as_view'

as_view方法是 Flask可插入视图的一部分, View类. Resource类建立在此类的顶部,而我的UserAPI类建立在Resource类的顶部.因此,应该继承as_view方法,但不能继承.

The as_view method is part of the Flask Pluggable Views, this is, the View class. The Resource class is built on top of this class, and my UserAPI class on top of the Resource class. For this reason, the as_view method should be inherited, but it is not.

关于哪个可能是问题的任何想法?

Any ideas on which could be the problem?

在这里您可以看到我如何定义类:

Here you can see how I defined the classes:

from app import api, db
from flask.ext.restful import Resource, abort
from models import *

class UserAPI(Resource):

    def get(self, user_id):
            user = User.query.filter(User.id == user_id)[0]
            if user is None:
                    abort(404, message="User {} doesn't exist".format(user_id))

            else:
                    return {'user' : user}, 201

api.add_resource(UserAPI, '/api/v0.1/users/<int:user_id>', endpoint = 'user')

这是完整的追溯:

Traceback (most recent call last):
  File "/home/app/queries.py", line 35, in <module>
    api.add_resource(UserAPI, '/api/v0.1/users/<int:user_id>', endpoint = 'user')
  File "/home/app_env/local/lib/python2.7/site-packages/flask_restful/__init__.py", line 344, in add_resource
    self._register_view(self.app, resource, *urls, **kwargs)
  File "/home/app_env/local/lib/python2.7/site-packages/flask_restful/__init__.py", line 362, in _register_view
    resource_func = self.output(resource.as_view(endpoint))
AttributeError: type object 'UserAPI' has no attribute 'as_view'

在这里您可以看到ViewResource类如何具有as_view方法,而我的UserAPI类却没有:

Here you can see how the View and Resource classes have the as_view method, while my UserAPI class has not:

>>> import inspect
>>> from flask.views import View
>>> inspect.getmembers(View, predicate=inspect.ismethod)
[('as_view', <bound method type.as_view of <class 'flask.views.View'>>), ('dispatch_request', <unbound method View.dispatch_request>)]
>>>
>>> from flask.ext.restful import Resource
>>> inspect.getmembers(Resource, predicate=inspect.ismethod)
[('as_view', <bound method MethodViewType.as_view of <class 'flask_restful.Resource'>>), ('dispatch_request', <unbound method Resource.dispatch_request>)]
>>>
>>> from app.queries import UserAPI
>>> inspect.getmembers(UserAPI, predicate = inspect.ismethod)
[('__init__', <unbound method UserAPI.__init__>), ('__repr__', <unbound method UserAPI.__repr__>), ('get', <unbound method UserAPI.get>)]

推荐答案

扫描代码后,我发现Resource类继承自Flask的MethodView类.好吧,最后我设法通过直接从MethodView类而不是Resource类继承来获取as_view方法.这是:

After scanning the code I found that the Resource class inherits from the MethodView class of Flask. Well, finally I managed to get the as_view method by inheriting directly from the MethodView class instead of the Resource class. This is:

from app import api, db
from flask.ext.restful import abort
from flask.views import MethodView
from models import *

class UserAPI(MethodView):

     def get(self, user_id):
         user = User.query.filter(User.id == user_id)[0]
         if user is None:
             abort(404, message="User {} doesn't exist".format(user_id))
         else:
             return {'user' : user}, 201

api.add_resource(UserAPI, '/api/v0.1/users/<int:user_id>', endpoint = 'user')

我希望有人觉得这有帮助.

I hope someone finds this helpful.

这篇关于Flask-Restful错误:"as_view"方法未继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆