Django,如何在管理界面中查看会话数据

发布于 2024-10-17 14:01:48 字数 768 浏览 3 评论 0原文

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

即,对于每个会话,我想查看存储在会话数据库中的数据(据我所知,它本质上是一本字典)。

目前我只能在 Session data 字段中看到哈希,例如:

gAJ9cQEoVQ5zb3J0aW5nX2Nob2ljZXECVQJQT3EDVQxnYW1lc19wbGF5ZWRxBH1xBVgLAAAAcG9z
dG1hbi1wYXRxBksDc1UKaXBfYWRkcmVzc3EHVQkxMjcuMC4wLjFxCFUKdGVzdGNvb2tpZXEJVQZ3
b3JrZWRxClUKZ2FtZV92b3Rlc3ELfXEMdS4wOGJlMDY3YWI0ZmU0ODBmOGZlOTczZTUwYmYwYjE5
OA==


我已将以下内容放入 admin.py 中以实现此目的:

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


特别是我希望能够至少查看每个会话的 IP 地址。 (如果我可以计算每个 IP 地址有多少个会话,并根据每个 IP 地址的总会话数对 IP 进行排序,那就太好了。)

感谢您的帮助:-)

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==

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

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

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 :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蓝眼泪 2024-10-24 14:01:48

你可以这样做:

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中使用。如果存在一些问题导致其无法正常工作,您可以根据链接的 Django 源代码自行解码会话数据。

You can do something like this:

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)

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.

九局 2024-10-24 14:01:48

继续托马斯的回答,我继续说:

import pprint
from django.contrib.sessions.models import Session
class SessionAdmin(admin.ModelAdmin):
    def _session_data(self, obj):
        return pprint.pformat(obj.get_decoded()).replace('\n', '<br>\n')
    _session_data.allow_tags=True
    list_display = ['session_key', '_session_data', 'expire_date']
    readonly_fields = ['_session_data']
    exclude = ['session_data']
    date_hierarchy='expire_date'
admin.site.register(Session, SessionAdmin)

Continuing from Tomasz's answer, I went with:

import pprint
from django.contrib.sessions.models import Session
class SessionAdmin(admin.ModelAdmin):
    def _session_data(self, obj):
        return pprint.pformat(obj.get_decoded()).replace('\n', '<br>\n')
    _session_data.allow_tags=True
    list_display = ['session_key', '_session_data', 'expire_date']
    readonly_fields = ['_session_data']
    exclude = ['session_data']
    date_hierarchy='expire_date'
admin.site.register(Session, SessionAdmin)
乜一 2024-10-24 14:01:48

会话数据包含在 base64 编码的 pickled 字典中。这就是您在管理中看到的内容,因为该数据存储在会话模型中的 TextField 中。

我认为任何分布式 Django 代码都不会在会话中存储 IP 地址,但如果您可以访问它,您可以自己执行此操作。

为了显示真实的会话信息,您可以编写自己的表单字段来显示解码的信息。请记住,如果您想修改 save 方法,则还必须覆盖它。您可以在 django/contrib/sessions/models.py 中查看编码和解码方法。

Session data is contained in a base64 encoded pickled dictionary. That's is what you're seeing in the admin because that data is stored in a TextField in the Session model.

I don't think any distributed Django code stores the IP address in the session but you could do it yourself if you can access it.

In order to display the real session information, you may write your own form field that presents the decoded information. Keep in mind that you'll have to also overwrite the save method if you want to modify it. You can take a look at the encode and decode methods in django/contrib/sessions/models.py.

甜中书 2024-10-24 14:01:48

EB 的其他很好的答案给我留下了错误“数据库在 QuerySet.dates() 中返回了无效值。时区定义和 pytz 安装了吗?”。 (我确实安装了 db tz info 和 pytz,并且我的应用程序广泛使用时区。)删除“date_hierarchy”行为我解决了这个问题。所以:

import pprint
from django.contrib.sessions.models import Session
class SessionAdmin(admin.ModelAdmin):
    def _session_data(self, obj):
        return pprint.pformat(obj.get_decoded()).replace('\n', '<br>\n')
    _session_data.allow_tags=True
    list_display = ['session_key', '_session_data', 'expire_date']
    readonly_fields = ['_session_data']
    exclude = ['session_data']
admin.site.register(Session, SessionAdmin)

EB's otherwise great answer left me with the error "Database returned an invalid value in QuerySet.dates(). Are time zone definitions and pytz installed?". (I do have db tz info and pytz installed, and my app uses timezones extensively.) Removing the 'date_hierarchy' line resolved the issue for me. So:

import pprint
from django.contrib.sessions.models import Session
class SessionAdmin(admin.ModelAdmin):
    def _session_data(self, obj):
        return pprint.pformat(obj.get_decoded()).replace('\n', '<br>\n')
    _session_data.allow_tags=True
    list_display = ['session_key', '_session_data', 'expire_date']
    readonly_fields = ['_session_data']
    exclude = ['session_data']
admin.site.register(Session, SessionAdmin)
在风中等你 2024-10-24 14:01:48

除了之前的答案之外,我们还可以向该会话的用户显示这有助于识别用户的会话。

class SessionAdmin(admin.ModelAdmin):
    def user(self, obj):
        session_user = obj.get_decoded().get('_auth_user_id')
        user = User.objects.get(pk=session_user)
        return user.email
    def _session_data(self, obj):
        return pprint.pformat(obj.get_decoded()).replace('\n', '<br>\n')
    _session_data.allow_tags = True
    list_display = ['user', 'session_key', '_session_data', 'expire_date']
    readonly_fields = ['_session_data']

Adding to previous answers, We can also show the user for that session which is helpful for identifying the session of users.

class SessionAdmin(admin.ModelAdmin):
    def user(self, obj):
        session_user = obj.get_decoded().get('_auth_user_id')
        user = User.objects.get(pk=session_user)
        return user.email
    def _session_data(self, obj):
        return pprint.pformat(obj.get_decoded()).replace('\n', '<br>\n')
    _session_data.allow_tags = True
    list_display = ['user', 'session_key', '_session_data', 'expire_date']
    readonly_fields = ['_session_data']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文