2 | | > For Django database sessions, there is no way to retrieve the datetime that the session will expire without directly querying the model. The model stores this value in the `expire_date` field, but it is not able to be retrieved from the session (i.e. `request.session` SessionStore) object. This makes it complicated to implement sliding expiration sessions if the sessions are not being modified by some other means. |
3 | | > |
4 | | > Of course, we could set `SESSION_SAVE_EVERY_REQUEST = True` to cause the session to be modified on every request, but this is inefficient. |
5 | | > |
6 | | > Currently, to retrieve the `expire_date` from a request's session we could do this. (Taken from the docs https://docs.djangoproject.com/en/3.1/topics/http/sessions/#using-sessions-out-of-views) |
7 | | > |
8 | | > {{{#!python |
9 | | > >>> from django.contrib.sessions.models import Session |
10 | | > >>> s = Session.objects.get(pk='2b1189a188b44ad18c35e113ac6ceead') |
11 | | > >>> s.expire_date |
12 | | > datetime.datetime(2005, 8, 20, 13, 35, 12) |
13 | | > }}} |
14 | | > |
15 | | > This is not ideal since it requires an extra query to the database and if we are in the request lifecycle we already have access to the session via `request.session`. The ideal solution would be if `django.contrib.sessions.backends.db.SessionStore` would make `expire_date` available since it has already queried the session database record in the `load()` function. |