简体   繁体   English

AttributeError: 类型对象“用户”没有属性“获取”

[英]AttributeError: type object 'User' has no attribute 'get'

I'm trying to run my Flask Application and I keep getting the following Error:我正在尝试运行 Flask 应用程序,但不断收到以下错误:

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

I'm assuming it's coming from my Models.py code:我假设它来自我的 Models.py 代码:

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'

I just had this problem.我刚遇到这个问题。 shmee's proposed solution is what worked for me: shmee 提出的解决方案对我有用:

  1. The load_user() function should not be a part of the User class, but instead its own standalone function. load_user()函数不应该是User类的一部分,而是它自己的独立函数。
  2. The load_user() function should call User.query.get(user_id) , not User.get(user_id) . load_user()函数应该调用User.query.get(user_id) ,而不是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) .我认为这种混淆来自 Flask-Login 的文档从今天起采用后一种方式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)

Thank you @Nathan Wailes and @shmee.谢谢@Nathan Wailes 和@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: @will - 最好为 User 模型继承 UserMixin 类和 db.Model 类,而不是显式编写以下方法的代码:

  • is_active活跃
  • is_authenticated is_authenticated
  • is_anonymous is_anonymous
  • get_id get_id
class User(UserMixin,db.Modle):
    __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:然后在 app.py(主应用程序类)中,您可以在调用登录、注销等..端点之前添加以下代码:

@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/您可以在此处参考 Flask-Login 文档: https : //flask-login.readthedocs.io/en/latest/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flask-Login: AttributeError: type object 'User' 没有属性 'get' - Flask-Login: AttributeError: type object 'User' has no attribute 'get' AttributeError: 类型对象“用户”没有属性“查询” - AttributeError: type object 'User' has no attribute 'query' AttributeError: 类型对象“Message”没有属性“get” - AttributeError: type object 'Message' has no attribute 'get' /notify/ &#39;User&#39; 对象的 AttributeError 没有属性 &#39;get&#39; - AttributeError at /notify/ 'User' object has no attribute 'get' Django: AttributeError: 'User' object 没有属性 'get' - Django: AttributeError: 'User' object has no attribute 'get' AttributeError类型的对象没有属性 - AttributeError type object has no attribute AttributeError:类型对象“ User”没有属性“ _query_cls” - AttributeError: type object 'User' has no attribute '_query_cls' AttributeError: 类型对象“User”没有属性“USERNAME_FIELD” - AttributeError: type object 'User' has no attribute 'USERNAME_FIELD' django-AttributeError:类型对象“文件”没有属性“ set_user” - django - AttributeError: type object 'file' has no attribute 'set_user' SQLAlchemy告诉我“AttributeError:type object&#39;User&#39;没有属性&#39;columns&#39;” - SQLAlchemy show me that “AttributeError: type object 'User' has no attribute 'columns'”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM