Django,如何在管理界面中查看会话数据 [英] Django, how to see session data in the admin interface

查看:127
本文介绍了Django,如何在管理界面中查看会话数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Django会话,我想要一种查看会话的方式数据在管理界面。这是可能的吗?

I'm using Django sessions and I would like a way of seeing the session data in the admin interface. Is this possible?

对于每个会话,我想看到存储在会话数据库中的数据(这本质上是一个可以收集的字典)。

I.e. for each session I want to see the data stored in the session database (which is essentially a dictionary as far as I can gather).

目前我可以看到一个哈希在会话数据字段中,例如:

Currently I can just see a hash in the Session data field, such as:

gAJ9cQEoVQ5zb3J0aW5nX2Nob2ljZXECVQJQT3EDVQxnYW1lc19wbGF5ZWRxBH1xBVgLAAAAcG9z
dG1hbi1wYXRxBksDc1UKaXBfYWRkcmVzc3EHVQkxMjcuMC4wLjFxCFUKdGVzdGNvb2tpZXEJVQZ3
b3JrZWRxClUKZ2FtZV92b3Rlc3ELfXEMdS4wOGJlMDY3YWI0ZmU0ODBmOGZlOTczZTUwYmYwYjE5
OA==


我将以下内容放在admin.py中:

I have put the following into admin.py to achieve this:

from django.contrib.sessions.models import Session
...
admin.site.register(Session)


特别是我希望能够看到每个会话至少有一个IP地址。 (如果我可以计算每个IP地址多少个会话,并且根据每个会话的总数排序IP,那么也会很好)。

In particular I was hoping to be able to see at least an IP address for each session. (Would be nice too if I could count how many sessions per IP address and order the IPs based on number of sessions in total for each.)

感谢您的帮助: - )

Thank you for your help :-)

推荐答案

你可以这样做:

from django.contrib.sessions.models import Session
class SessionAdmin(ModelAdmin):
    def _session_data(self, obj):
        return obj.get_decoded()
    list_display = ['session_key', '_session_data', 'expire_date']
admin.site.register(Session, SessionAdmin)

甚至可能 get_decoded 可以直接在 list_display 中使用。如果有一些catch可以阻止这个工作正常,你可以根据链接的Django源来自己解码会话数据。

It might be even that get_decoded can be used directly in list_display. And in case there's some catch that prevents this from working ok, you can decode the session data yourself, based on the linked Django source.

这篇关于Django,如何在管理界面中查看会话数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆