如何从具有'pk':50的会话中删除对象并将其存储到所有页面。当我修改请求会话时,我将SESSION_SAVE_EVERY_REQUEST = True添加到Settings.py并将request.session.modified = True添加到上面的行,但没有效果。
Json看起来像
[
{
"pk": 50,
"model": "notifications.notification",
"fields": {
"recipient": 81,
"verb": "commented",
"emailed": false,
"action_object_object_id": "",
"level": "info",
"deleted": false,
"timestamp": "2017-01-25T11:18:53.197Z",
"target_content_type": null,
"actor_object_id": "1",
"action_object_content_type": null,
"target_object_id": "790",
"actor_content_type": 3,
"unread": true,
"data": "\"\"",
"public": true,
"description": "commented on your request"
}
},
{
"pk": 38,
"model": "notifications.notification",
"fields": {
"recipient": 81,
"verb": "commented",
"emailed": false,
"action_object_object_id": "",
"level": "info",
"deleted": false,
"timestamp": "2017-01-24T12:23:08Z",
"target_content_type": null,
"actor_object_id": "1",
"action_object_content_type": null,
"target_object_id": "790",
"actor_content_type": 3,
"unread": true,
"data": "\"\"",
"public": true,
"description": "commented on your request"
}
}
]
def setNotifRead(request, notif_id):
notifObject = Notification.objects.filter(pk=notif_id)
notifObject.update(unread=0)
notifications = request.session['notifications']
request.session.modified = True
del notifications[notif_id] # Something should be done here
return
答案 0 :(得分:0)
您需要遍历数据并过滤不具备相关ID的项目。
notifications = [item for item in request.session['notifications'] if item['id'] != notif_id
request.session['notifications'] = notifications