Flask-Restful error: "as_view" method not inherited
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
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.
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.