Flask - AttributeError: 'module' object has no attribute 'items'

utkbansal

I am using flask-restful and have the following API class:

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

I'm facing a AttributeError: 'module' object has no attribute 'items'.

The traceback is:

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'

Looking at similar questions, this might be a circular dependency problem, but I'm unable to figure it out. So I have included all the imports of both the files above.

EDIT

The error may be on the return line of the get method, in using the marshall function.

Mike Müller

fields is a module, imported here:

from flask.ext.restful import fields, marshal

Therefore, this hands marshal a module:

def get(self, post_id):
    comments = Post.query.get_or_404(post_id).comments.all()
    return marshal(comments, fields)

You likely want to use self.fields:

def get(self, post_id):
    comments = Post.query.get_or_404(post_id).comments.all()
    return marshal(comments, self.fields)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Flask Blueprint AttributeError: 'module' object has no attribute 'name' error

From Dev

AttributeError: 'module' object has no attribute

From Dev

AttributeError: 'module' object has no attribute

From Dev

AttributeError: 'module' object has no attribute

From Dev

AttributeError: 'str' object has no attribute 'items'

From Dev

AttributeError: 'set' object has no attribute 'items'

From Dev

Python - AttributeError: 'str' object has no attribute 'items'

From Dev

AttributeError: 'list' object has no attribute 'items' in a scrapy

From Dev

AttributeError: 'bool' object has no attribute 'items'

From Dev

AttributeError: '_AppCtxGlobals' object has no attribute 'user' in Flask

From Dev

Flask AttributeError: 'NoneType' object has no attribute 'split'

From Dev

Flask - AttributeError: '_AppCtxGlobals' object has no attribute 'db'

From Dev

AttributeError:'module' object has no attribute 'call' :Python

From Dev

AttributeError: 'module' object has no attribute 'tk'

From Dev

AttributeError: 'module' object has no attribute 'cache'

From Java

AttributeError: 'module' object has no attribute 'tests'

From Dev

AttributeError: 'module' object has no attribute 'TestCase'

From Dev

AttributeError: 'module' object has no attribute 'version'

From Dev

Mtaplotlib AttributeError: 'module' object has no attribute 'pyplot'

From Dev

AttributeError: 'module' object has no attribute 'plotting'

From Java

AttributeError: 'module' object has no attribute 'request'

From Java

tensorflow:AttributeError: 'module' object has no attribute 'mul'

From Java

AttributeError: 'module' object has no attribute 'urlretrieve'

From Dev

AttributeError: 'module' object has no attribute 'grd'

From Dev

AttributeError: 'module' object has no attribute 'SignedJwtAssertionCredentials'

From Dev

AttributeError: 'module' object has no attribute 'scandir'

From Dev

AttributeError: 'module' object has no attribute 'SFrame'

From Dev

Pylint AttributeError: 'module' object has no attribute 'append'

From Dev

Inheritance AttributeError: 'module' object has no attribute

Related Related

  1. 1

    Flask Blueprint AttributeError: 'module' object has no attribute 'name' error

  2. 2

    AttributeError: 'module' object has no attribute

  3. 3

    AttributeError: 'module' object has no attribute

  4. 4

    AttributeError: 'module' object has no attribute

  5. 5

    AttributeError: 'str' object has no attribute 'items'

  6. 6

    AttributeError: 'set' object has no attribute 'items'

  7. 7

    Python - AttributeError: 'str' object has no attribute 'items'

  8. 8

    AttributeError: 'list' object has no attribute 'items' in a scrapy

  9. 9

    AttributeError: 'bool' object has no attribute 'items'

  10. 10

    AttributeError: '_AppCtxGlobals' object has no attribute 'user' in Flask

  11. 11

    Flask AttributeError: 'NoneType' object has no attribute 'split'

  12. 12

    Flask - AttributeError: '_AppCtxGlobals' object has no attribute 'db'

  13. 13

    AttributeError:'module' object has no attribute 'call' :Python

  14. 14

    AttributeError: 'module' object has no attribute 'tk'

  15. 15

    AttributeError: 'module' object has no attribute 'cache'

  16. 16

    AttributeError: 'module' object has no attribute 'tests'

  17. 17

    AttributeError: 'module' object has no attribute 'TestCase'

  18. 18

    AttributeError: 'module' object has no attribute 'version'

  19. 19

    Mtaplotlib AttributeError: 'module' object has no attribute 'pyplot'

  20. 20

    AttributeError: 'module' object has no attribute 'plotting'

  21. 21

    AttributeError: 'module' object has no attribute 'request'

  22. 22

    tensorflow:AttributeError: 'module' object has no attribute 'mul'

  23. 23

    AttributeError: 'module' object has no attribute 'urlretrieve'

  24. 24

    AttributeError: 'module' object has no attribute 'grd'

  25. 25

    AttributeError: 'module' object has no attribute 'SignedJwtAssertionCredentials'

  26. 26

    AttributeError: 'module' object has no attribute 'scandir'

  27. 27

    AttributeError: 'module' object has no attribute 'SFrame'

  28. 28

    Pylint AttributeError: 'module' object has no attribute 'append'

  29. 29

    Inheritance AttributeError: 'module' object has no attribute

HotTag

Archive