我正在使用flask-restful并拥有以下API类:
views.py
from datetime import date
from flask import jsonify
from flask.ext.restful import Resource, reqparse
from backend import db
from .models import User, Post, Comment, WorkExperience
from flask.ext.restful import fields, marshal
from backend.helpers import AuthRequiredResource, UserMixin
class CommentList(UserMixin, AuthRequiredResource):
def __init__(self):
self.fields = {
'id': fields.Integer,
'body': fields.String,
'added_on': fields.DateTime
}
self.parser = reqparse.RequestParser()
self.parser.add_argument('body')
super(CommentList, self).__init__()
def get(self, post_id):
comments = Post.query.get_or_404(post_id).comments.all()
# APPEARS THAT THE ERROR ARISES HERE
return marshal(comments, fields)
def post(self, post_id):
args = self.parser.parse_args()
user = self.get_user()
c = Comment(post_id, args['body'], user.id)
db.session.add(c)
db.session.commit()
helper.py
from flask import request
from flask.ext.restful import Resource
from backend.models import User
class UserMixin(object):
@staticmethod
def get_user():
user = User.query.filter_by(
auth_token=request.headers.environ.get('HTTP_AUTHORIZATION')
).first()
if user is None:
return {'message': 'Invalid auth token'}, 401
return user
class AuthRequiredResource(Resource):
def dispatch_request(self, *args, **kwargs):
if request.headers.environ.get('HTTP_AUTHORIZATION', None) is not None:
return super(AuthRequiredResource, self).dispatch_request(*args,
**kwargs)
else:
return {'message': 'Auth Token missing'}, 401
我面对AttributeError: 'module' object has no attribute 'items'
。
追溯是:
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/utkbansal/.virtualenvs/cv_backend/lib/python2.7/site-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/utkbansal/.virtualenvs/cv_backend/lib/python2.7/site-packages/flask_restful/__init__.py", line 268, in error_router
return self.handle_error(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/utkbansal/.virtualenvs/cv_backend/lib/python2.7/site-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/utkbansal/.virtualenvs/cv_backend/lib/python2.7/site-packages/flask_restful/__init__.py", line 268, in error_router
return self.handle_error(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/utkbansal/.virtualenvs/cv_backend/lib/python2.7/site-packages/flask_restful/__init__.py", line 477, in wrapper
resp = resource(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/home/utkbansal/PycharmProjects/cv_backend/backend/helpers.py", line 12, in dispatch_request
**kwargs)
File "/home/utkbansal/.virtualenvs/cv_backend/lib/python2.7/site-packages/flask_restful/__init__.py", line 587, in dispatch_request
resp = meth(*args, **kwargs)
File "/home/utkbansal/PycharmProjects/cv_backend/backend/views.py", line 130, in get
return marshal(comments, fields)
File "/home/utkbansal/.virtualenvs/cv_backend/lib/python2.7/site-packages/flask_restful/__init__.py", line 635, in marshal
if envelope else [marshal(d, fields) for d in data])
File "/home/utkbansal/.virtualenvs/cv_backend/lib/python2.7/site-packages/flask_restful/__init__.py", line 639, in marshal
for k, v in fields.items())
AttributeError: 'module' object has no attribute 'items'
看一下类似的问题,这可能是循环依赖问题,但我无法弄清楚。所以我已经包含了上述两个文件的所有导入。
使用马歇尔函数时,错误可能出在get方法的返回行上。
答案 0 :(得分:3)
fields
是一个模块,导入到此处:
from flask.ext.restful import fields, marshal
因此,这需要marshal
一个模块:
def get(self, post_id):
comments = Post.query.get_or_404(post_id).comments.all()
return marshal(comments, fields)
您可能想要使用self.fields
:
def get(self, post_id):
comments = Post.query.get_or_404(post_id).comments.all()
return marshal(comments, self.fields)