我正在爲網站創建簡單的通知系統。用戶的通知從數據庫中提取,如果尚未顯示,則會標記爲已顯示,然後顯示給用戶。那些未被看見的以粗體顯示。下面是我的一些代碼:停止SQLAlchemy更新已從數據庫中提取的行
query = request.db.query(Notification)\
.filter(Notification.user == request.user)
notifications = query.order_by(Notification.created_at.desc()).all()
print [ notif.seen for notif in notifications ] # [ False, False, False... ]
query.filter(Notification.seen == False).update({
'seen': True
})
request.db.commit()
print [ notif.seen for notif in notifications ] # [ True, True, True... ]
你會從我的打印報表時執行update
查詢,儘管已經被從數據庫中抽取與.all()
注意到notifications
被修改。
我不想要這種行爲。我需要看看什麼notifications
是,而不是它是,以便大膽的領域,以前是不可見的。
通過文檔查看,我認爲設置synchronize_session
參數爲False
可能工作。
query.filter(Notification.seen == False).update({
'seen': True
}, False)
但不幸的是,它沒有。
我該如何解決這個問題?
是的,關掉了'expire_on_commit',它工作完美。 –