Flask-Restful error: "as_view" method not inherited
flask, flask-restful, python, python-2.7
Solution
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.
Problem
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' ``` 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') ``` EDIT: This the full traceback: ``` 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' ``` 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>)] ```