2013-05-13 11 views
5

Flaskを使用してフォーラムプロジェクトを作成し、Flask-SQLAlchemyを使用してすべてのユーザー、スレッド、投稿などを管理しています。しかし、xを行う(たとえば投稿を編集する)と、何か他のことをしようとすると(例えば投稿を削除する)InvalidRequestErrorが発生することが判明しました。記事を編集するためのFlask-SQLAlchemy 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: 
    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)) 

とポストに残念ながら

@app.route('/forum/id=<id>/thr=<t_id>', methods=['GET','POST']) 
def thread(id, t_id): 
    forum = Forum.query.filter_by(id=id).first() 
    thread = Thread.query.filter_by(id=t_id).first() 
    posts = Post.query.filter_by(thread=thread).all() 
    form = PostForm() 
    if form.validate_on_submit(): 
    post = Post(body=form.body.data, 
       timestamp=datetime.utcnow(), 
       thread=thread, 
       author=g.user) 
    db.session.add(post) 
    db.session.commit() 
    return redirect(url_for('thread', id=id, t_id=t_id)) 
    return render_template('thread.html', forum=forum, thread=thread, posts=posts, form=form, title=thread.title) 

、この問題の解決を作成するための唯一の確実な方法を掲示実際にアプリを実行するスクリプトをリセットすることです。run.py

#!bin/python 

from app import app 
app.run(debug=True,host='0.0.0.0') 

答えて

3

あなたの問題の一部である可能性があるため、WooshAlchemyを使用していますか? Described here

彼は、WooshAlchemy拡張の修正が必要な「修正」について説明します。

通常、ポストモデルオブジェクトを呼び出し、「session.add」を使用して接続してから「session.delete」を試行したり、同じオブジェクトに対して別の「session.add」を実行したりする可能性があります。

また、あなたのリクエストルーティングはフラスコにとってちょっと変わっています。以前はFlaskで "thr = <t_id>"タイプの表記を見たことがありませんでした。それはあなたのためにうまくいっていますか?

http://flask.pocoo.org/docs/quickstart/#variable-rules

+0

WhooshAlchemyが本当に問題だったように見えます。表記法に関しては、単に「thr = 」の略語です。 – Ganye

+0

私は "thr ="の部分を意味しました。しかし、あなたは/ thr = 44/p = 32/c = 21のURL形式をやることができると思っています。私は問題を理解してうれしいです。 – Dexter

+0

それはまさにそれが何であるかです。たとえば、フォーラム(1)の特定のスレッド(8)がURL「/ forum/id = 1/thr = 8」を作成します。 – Ganye

0

編集投稿が正しく行われていないと思います。 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)) 
関連する問題