gpt4 book ai didi

python - 属性错误 : type object 'User' has no attribute 'get'

转载 作者:行者123 更新时间:2023-12-05 08:41:15 26 4
gpt4 key购买 nike

我正在尝试运行我的 Flask 应用程序,但我不断收到以下错误:

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

我假设它来 self 的 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)

玩玩之后,看起来我可能需要在某处使用以下代码块或以下代码块的变体?:

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

或者它的某种类型的衍生物。任何帮助将不胜感激。谢谢!

这是完整的回溯:

 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'

最佳答案

我刚遇到这个问题。 shmee 提出的解决方案对我有用:

  1. load_user() 函数不应该是 User 类的一部分,而是它自己的独立函数。
  2. load_user() 函数应该调用 User.query.get(user_id),而不是 User.get(user_id)
    • 我认为这种混淆是因为 Flask-Login 的文档 has it the latter way从今天开始:return User.get(user_id)

示例代码:

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)

关于python - 属性错误 : type object 'User' has no attribute 'get' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51209763/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com