gpt4 book ai didi

python - TypeError : sqlalchemy. orm.attributes.InstrumentedAttribute 对象位于 0x7f86789f9bf8 不可 JSON 序列化

转载 作者:行者123 更新时间:2023-12-01 17:31:17 28 4
gpt4 key购买 nike

我使用 Flask 和 Flask-Login 进行用户身份验证。 Flask-Sqlalchemy 将这些模型存储在 sqlite 数据库中:

ROLE_USER = 0
ROLE_ADMIN = 1

class Anonymous(AnonymousUserMixin):
def __init__(self):
self.username = 'Guest'

lm.anonymous_user = Anonymous

class User(UserMixin, db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key = True)
username = db.Column(db.String(64), index = True, unique = True)
password = db.Column(db.String(20), index = False, unique = True)
email = db.Column(db.String(120), index = True, unique = True)
registered_on = db.Column('registered_on' , db.DateTime)
role = db.Column(db.SmallInteger, default = ROLE_USER)

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

def get_id(self):
return User.id


class PostVote(db.Model):
__tablename__ = "postvotes"
post_id = db.Column(db.Integer, db.ForeignKey('posts.id'), primary_key = True)
user_id = db.Column(db.Integer, default = 0)
type = db.Column(db.Boolean, default = True)

class Post(db.Model):
__tablename__ = "posts"
id = db.Column(db.Integer, primary_key = True)
body = db.Column(db.String(320))
points = db.Column(db.Integer, default = 0)
votes = db.relationship(PostVote)
timestamp = db.Column(db.DateTime)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))

def __repr__(self):
return '<Post %r>' % self.body

还有我的登录代码:

@app.route('/login', methods = ['GET', 'POST'])
def init_login():
form = LoginForm()
if request.method == 'GET':
return render_template('login/index.html', form = form, active = 'login')
if form.validate():
uname = form.username.data
passw = form.password.data
print('DBG: Loginning ' + uname) #for debug purposes
registered_user = User.query.filter_by(username = uname).first()
if registered_user is None:
flash('User not found')
return render_template('/login/index.html', form=form,
msgtype = 'alert-warning', active = 'login')
if check_passw(passw, registered_user.password):
login_user(registered_user)
session['username'] = uname
return redirect(url_for('init_login_success'))
else:
flash('Incorrect password')
return render_template('/login/index.html', form=form,
msgtype = 'alert-danger', active = 'login')
return render_template('login/index.html', form = form, active = 'login')

当我尝试登录时,SQLAlchemy 引发错误,您可以在帖子标题 ( TypeError: <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x7f86789f9bf8> is not JSON serializable ) 中看到该错误。 AFAIK,InstumentedAttribute 用于关系,但它对我没有帮助。
我还发现执行login_user() 时发生错误。无需登录即可正常注册。
我该如何修复它?

最佳答案

现在 get_id() 在文档中同时返回一个整数:

get_id()
This method must return a unicode that uniquely identifies this user, and can be used to load the user from the user_loader callback. Note that this must be a unicode - if the ID is natively an int or some other type, you will need to convert it to unicode.

我写这个是为了解决这个问题:

def get_id(self):
return str(self.id)

关于python - TypeError : sqlalchemy. orm.attributes.InstrumentedAttribute 对象位于 0x7f86789f9bf8 不可 JSON 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41329000/

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