Flask SQLAlchemy InvalidRequestError:对象已附加到会话
我正在使用Flask创建一个论坛项目,并使用Flask SQLAlchemy管理所有用户、线程、帖子等。然而,我发现当我尝试做x(例如编辑帖子)时,如果我尝试做其他事情(例如删除帖子),我会得到一个InvalidRequestError 编辑一篇文章Flask SQLAlchemy InvalidRequestError:对象已附加到会话,flask,flask-sqlalchemy,Flask,Flask Sqlalchemy,我正在使用Flask创建一个论坛项目,并使用Flask SQLAlchemy管理所有用户、线程、帖子等。然而,我发现当我尝试做x(例如编辑帖子)时,如果我尝试做其他事情(例如删除帖子),我会得到一个InvalidRequestError 编辑一篇文章 def post_edit(id, t_id, p_id): post = Post.query.filter_by(id=p_id).first() if post.author.username == g.user.username:
def post_edit(id, t_id, p_id):
post = Post.query.filter_by(id=p_id).first()
if post.author.username == g.user.username:
form = PostForm(body=post.body)
if form.validate_on_submit():
post.body = form.body.data
db.session.commit()
return redirect(url_for('thread', id=id, t_id=t_id))
return render_template('post_edit.html', form=form, title='Edit')
else:
flash('Access denied.')
return redirect(url_for('thread', id=id, t_id=t_id))
@app.route('/forum=<id>/thr=<t_id>/p=<p_id>/delete', methods=['GET','POST'])
def post_delete(id, t_id, p_id):
post = Post.query.filter_by(id=p_id).first()
if post.author.username == g.user.username:
db.session.delete(post)
db.session.commit()
return redirect(url_for('thread', id=id, t_id=t_id))
else:
flash('Access denied.')
return redirect(url_for('thread', id=id, t_id=t_id))
以及删除一个帖子
def post_edit(id, t_id, p_id):
post = Post.query.filter_by(id=p_id).first()
if post.author.username == g.user.username:
form = PostForm(body=post.body)
if form.validate_on_submit():
post.body = form.body.data
db.session.commit()
return redirect(url_for('thread', id=id, t_id=t_id))
return render_template('post_edit.html', form=form, title='Edit')
else:
flash('Access denied.')
return redirect(url_for('thread', id=id, t_id=t_id))
@app.route('/forum=<id>/thr=<t_id>/p=<p_id>/delete', methods=['GET','POST'])
def post_delete(id, t_id, p_id):
post = Post.query.filter_by(id=p_id).first()
if post.author.username == g.user.username:
db.session.delete(post)
db.session.commit()
return redirect(url_for('thread', id=id, t_id=t_id))
else:
flash('Access denied.')
return redirect(url_for('thread', id=id, t_id=t_id))
我认为你的编辑文章做得不对。使用populate_obj函数
@app.route('/forum=<id>/thr=<t_id>/p=<p_id>/edit', methods=['GET','POST'])
def post_edit(id, t_id, p_id):
post = Post.query.filter_by(id=p_id).first()
if post.author.username == g.user.username:
form = PostForm(obj=post)
if form.validate_on_submit():
form.populate_obj(post)
db.session.commit()
return redirect(url_for('thread', id=id, t_id=t_id))
return render_template('post_edit.html', form=form, title='Edit')
else:
flash('Access denied.')
return redirect(url_for('thread', id=id, t_id=t_id))
@app.route('/forum=/thr=/p=/edit',methods=['GET','POST']))
def post_编辑(id、t_id、p_id):
post=post.query.filter_by(id=p_id).first()
如果post.author.username==g.user.username:
表单=PostForm(obj=post)
if form.validate_on_submit():
表格.填写(职位)
db.session.commit()
返回重定向(url_for('thread',id=id,t_id=t_id))
返回呈现模板('post\u edit.html',form=form,title='edit')
其他:
闪存(“访问被拒绝”)
返回重定向(url_for('thread',id=id,t_id=t_id))
您是否正在使用WooshAlchemy,因为这可能是您的问题的一部分
他描述了需要修改WooshAlchemy扩展的“修复”
通常,这可能意味着您调用了一个后期模型对象,然后使用“session.add”附加它,然后尝试“session.delete”,或者对同一对象执行了另一个“session.add”
另外,您的请求路由对于flask来说有点奇怪,我以前从未见过flask的“thr=”表示法类型。这对你有效吗
看来“嗖嗖”炼金术确实是个问题。至于表示法,它只是“thr=”的缩写。我指的是“thr=”部分。但我想你可以使用/thr=44/p=32/c=21这种url格式,我只是觉得很奇怪而已。我很高兴我们解决了这个问题,这正是问题所在;例如,论坛(1)中的特定线程(8)创建url“/forum/id=1/thr=8”。