在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python  數(shù)據(jù)庫/ Flask Sqlalchemy 總是提示:is not bound to a

Flask Sqlalchemy 總是提示:is not bound to a Session; attri

我在視圖函數(shù)中調用了模型中的一個靜態(tài)方法:

@staticmethod
def getDots(pid='0', result=[]):
    current = Dot.query.filter_by(pid = pid).all()
    for v in current:
        result.append(v)
        Dot.getDots(v.id, result)
    return list(result)

視圖函數(shù):


@dot.route('/list', methods=['post', 'get'])
@login_required
def list():
    dots = Dot.getDots()
    return render_template('dot/list.html', dots=dots)
    

第一次進入的時候數(shù)據(jù)都是正確的,如果再刷新一次就出現(xiàn)報錯:

clipboard.png

不知道為什么,第一次總是可以的,就是不能刷新

回答
編輯回答
編輯回答
嫑吢丕

我的理解是,你在你的 Dot 類中定義了一個即是靜態(tài)函數(shù)又是遞歸函數(shù)的 getDots()。這里涉及到你的遞歸問題。

出現(xiàn)以上錯誤的原因是因為:session 已經被提交,導致操作的 model 對象已經不在當前 session 中了。

使用下面的方法解決一下試試。

@staticmethod
def getDots(pid='0', result=[]):
    current = Dot.query.filter_by(pid = pid).all()
    for v in current:
        result.append(v)
        v = session.merge(v) # 新加內容
        Dot.getDots(v.id, result)
    return list(result)

或者你改變一下你的遞歸方法。

2017年10月20日 17:52