Django, how to see session data in the admin interface

后端 未结 4 1480
甜味超标
甜味超标 2021-02-18 19:09

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 store

4条回答
  •  庸人自扰
    2021-02-18 19:34

    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', '
    \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)

提交回复
热议问题