Django 会话删除 json 对象



如何从具有"pk":50 的会话中删除对象并将其存储到所有页面。当我修改请求会话时,我将 SESSION_SAVE_EVERY_REQUEST=True 添加到 Settings.py 和 request.session.modified = True 添加到正上方行,但没有效果。

杰森看起来像

[
{
    "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

需要循环访问数据并筛选没有相关 ID 的项目。

notifications = [item for item in request.session['notifications'] if item['id'] != notif_id
request.session['notifications'] = notifications

最新更新