【发布时间】:2013-07-02 21:11:44
【问题描述】:
我正在为网站创建一个简单的通知系统。用户的通知从数据库中提取,如果它们还没有被标记为已看到,然后显示给用户。看不见的以粗体显示。这是我的一些代码:
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 在执行 update 查询时被修改,尽管已经使用 .all() 从数据库中提取。
我不想要这种行为。我需要查看notifications 是,而不是是,以便将以前未见过的字段加粗。
查看文档,我认为将 synchronize_session 参数设置为 False 可能会起作用。
query.filter(Notification.seen == False).update({
'seen': True
}, False)
但不幸的是,它没有。
我该如何解决这个问题?
【问题讨论】:
标签: python orm sqlalchemy