2

I'm trying to run my Flask Application and I keep getting the following Error:

AttributeError: type object 'User' has no attribute 'get'

I'm assuming it's coming from my Models.py code:

class User(db.Model):
    __tablename__ = 'users'
    __table_args__ = {'extend_existing': True} 
    id = db.Column(db.Integer, primary_key = True)
    email = db.Column(db.String(64), unique=True, index=True)
    username = db.Column(db.String(64), unique=True, index=True)
    password = db.Column(db.String(128))
    confirmed = db.Column(db.Boolean, default=False)
    #posts = db.relationship('Post', lazy='dynamic')


    def is_active(self):
        return True

    def get_id(self):
        return self.email

    def is_authenticated(self):
        return self.authenticated

    def is_anonymous(self):
        return False


    @login_manager.user_loader
    def load_user(userid):

        return User.get(User.id==userid)

After playing around, it looks like I may need the following code block somewhere or a variation of?:

def get_object(self):
    return self.request.user

Or some type of derivative of that. Any assistance would be greatly appreciated. Thanks!

Here is the full traceback:

 form = PostForm()
[2018-07-06 11:08:23,652] ERROR in app: Exception on /index [GET]
Traceback (most recent call last):
File 
"/Users.../sqlalchemy/sql/elements.py", line 682, in __getattr__
return getattr(self.comparator, key)
AttributeError: 'Comparator' object has no attribute 'request'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/...python3.6/site-packages/flask/app.py", line 2292, in 
wsgi_app
response = self.full_dispatch_request()
File "/Users/...b/python3.6/site-packages/flask/app.py", line 1815, in 
full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/...python3.6/site-packages/flask/app.py", line 1718, in 
handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users//python3.6/site-packages/flask/_compat.py", line 35, in 
reraise
raise value
File "/Users/...python3.6/site-packages/flask/app.py", line 1813, in 
full_dispatch_request
rv = self.dispatch_request()
File "/Users/WilliamStevens//site-packages/flask/app.py", line 1799, in 
dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
 File "/Users//Documents/deplorable_snowflake/.../views.py", line 52, in 
index
return render_template('frontpage.html', form=form, posts=posts)
File "/Users/.../python3.6/site-packages/flask/templating.py", line 133, 
   in render_template
  ctx.app.update_template_context(context)
    File "/Users/W/python3.6/site-packages/flask/app.py", line 792, in 
   update_template_context
   context.update(func())
    File "/Users/.../flask_login/utils.py", line 368, in 
   _user_context_processor
   return dict(current_user=_get_user())
  File "/Users/.../python3.6/site-packages/flask_login/utils.py", line 
  335, 
  in _get_user
  current_app.login_manager._load_user()
  File "/Users/.../python3.6/site- 
  packages/flask_login/login_manager.py", 
  line 359, in _load_user
  return self.reload_user()
  File "/Users/.../lib/python3.6/site- 
  packages/flask_login/login_manager.py", line 321, in reload_user
   user = self.user_callback(user_id)
   File 
  "/Users/.../models.py", line 53, in load_user
  return User.get(User.id==userid)
  File 
 "/Users/WilliamStevens/.../models.py", line 47, in get
 return self.request.user
    File "/Users/...site-packages/sqlalchemy/sql/elements.py", line 688, 
  in 
 __getattr__
  key)
 AttributeError: Neither 'BinaryExpression' object nor 'Comparator' 
 object has an attribute 'request'
7
  • It looks like you should handle the user backend by yourself stackoverflow.com/questions/12075535/….... The load_user seems not part of User class, see also gist.github.com/bkdinoop/6698956 Commented Jul 6, 2018 at 11:57
  • is the load_user function part of your model as the indentation in your code suggests? Commented Jul 6, 2018 at 12:02
  • Here is the full traceback: Commented Jul 6, 2018 at 15:10
  • @shmee i'm not sure? how can i go about finding that out? Commented Jul 6, 2018 at 15:39
  • Huh? I was under the impression you wrote that code. Surely you will know if you added the load_user to your User class. Also, the traceback you posted is from after you implemented Joel's answer below and does no longer show the AttributeError you mentioned in your question. Commented Jul 6, 2018 at 15:48

3 Answers 3

9

I just had this problem. shmee's proposed solution is what worked for me:

  1. The load_user() function should not be a part of the User class, but instead its own standalone function.
  2. The load_user() function should call User.query.get(user_id), not User.get(user_id).
    • I think this confusion comes from the fact that Flask-Login's documentation has it the latter way as of today: return User.get(user_id).

Example code:

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    password = db.Column(db.String(80))

    def __repr__(self):
        return '<User %r>' % self.username


@login_manager.user_loader
def load_user(user_id):
    return User.query.get(user_id)
Sign up to request clarification or add additional context in comments.

Comments

1

Im working with SQLAlchemy. My User class looks like this

class User(db.Model, UserMixin):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(250), nullable=False)
    email = db.Column(db.String(250), nullable=False, unique=True)
    password = db.Column(db.String(250), nullable=False)

I had this, which gave me the error:

  @login_manager.user_loader
    def load_user(user_id):
        return User.get(id)

And I change it to this:

@login_manager.user_loader
def load_user(user_id):
    return User.query.filter_by(id=user_id).first()

The point of load_user function is to get User object, to load it. I really dont know why User.get(user_id) doesnt work, but I retrieve a row from the table the I usually do.

Comments

0

Thank you @Nathan Wailes and @shmee. These solutions fixed my issue too.

@will - it's better to inherit UserMixin class along with db.Model class for User model instead of explicitly writing the code for below methods:

  • is_active
  • is_authenticated
  • is_anonymous
  • get_id
class User(UserMixin,db.Model):
    __tablename__ = 'users'
    __table_args__ = {'extend_existing': True} 
    id = db.Column(db.Integer, primary_key = True)
    email = db.Column(db.String(64), unique=True, index=True)
    username = db.Column(db.String(64), unique=True, index=True)
    password = db.Column(db.String(128))
    confirmed = db.Column(db.Boolean, default=False)

Then in app.py (main application class), you can add below code before calling the login, logout, etc.., end points:

# It should be placed outside of the class
@login_manager.user_loader
def load_user(userid):
    return User.query.get(userid)

You can refer Flask-Login documentation here: https://flask-login.readthedocs.io/en/latest/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.