47👍
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.
16👍
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)
- [Django]-Django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. (django 2.0.1)(Python 3.6)
- [Django]-Django – How to use decorator in class-based view methods?
- [Django]-Is get_or_create() thread safe
4👍
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
.
- [Django]-Django development server reload takes too long
- [Django]-How to render an ordered dictionary in django templates?
- [Django]-Convert seconds to hh:mm:ss in Python
4👍
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)
- [Django]-Get first item of QuerySet in template
- [Django]-HTML Forms without actions
- [Django]-Django: dependencies reference nonexistent parent node
2👍
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']
- [Django]-Django – use reverse url mapping in settings
- [Django]-Rendering a value as text instead of field inside a Django Form
- [Django]-Django switching, for a block of code, switch the language so translations are done in one language