Question

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>)]
Was it helpful?

Solution 2

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.

OTHER TIPS

I had this same issue. For me it was an import error.

I had the following file structure:

app.py
resources/__init__.py
resources/SomeResource.py

In app.py, I had the following code:

from resources import SomeResource
# ...
api.add_resource(SomeResource, '/someresource')
# ...

The error was due to the import line. It should have been:

from resources.SomeResource import SomeResource

I had the same issue, but it was due to duplicate view name in regular flask views and rest-full Resource views.

For example in routes.py

@products.route('/api/products', methods=['GET'])
def items():
    "some code here"

In Rest-full apis.py

class Items(Resource):
    def get(self):
        "Some code here"


api.add_resource(Items, '/hello')

When i changed one of the name of view then its working fine for me.

Its an old thread, but if you really use f-restful package of that version (~0.8.0), than maybe could be a bug.

Otherwise, for every newcomer, use the new way of importing the Resource class: See the official docs

It says:

class flask_restful.Resource

while you shouldn't use as: from flask.ext.restful import Resource, what means

flask.ext.restful.Resource

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top