ウェブサイト用の簡単な通知システムを作成しています。ユーザーの通知はデータベースから取得され、まだ表示されていない場合は表示済みとしてマークされ、ユーザーに表示されます。見られなかったものは太字で表示されます。これが私のコードの一部です:
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... ]
でデータベースから既にプルされているにもかかわらずnotifications
、クエリが実行されると変更される私の print ステートメントから気付くでしょう。update
.all()
私はこの動作をしたくありません。以前は見られなかったフィールドを太字にするために、それが何であるかではなく、何notifications
があったかを確認する必要があります。
ドキュメントを見て、synchronize_session
引数をに設定するとFalse
うまくいくかもしれないと思いました。
query.filter(Notification.seen == False).update({
'seen': True
}, False)
しかし、残念ながらそうではありませんでした。
どうすればこれを修正できますか?