Changes between Initial Version and Version 2 of Ticket #32088


Ignore:
Timestamp:
Oct 10, 2020, 9:19:13 AM (4 years ago)
Author:
Nate Pinchot
Comment:

Replying to Pallav Parikh:

Replying to Nate Pinchot:

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 object. This makes it complicated to implement sliding expiration sessions if the sessions are not being modified by some other means.

Of course, we could set SESSION_SAVE_EVERY_REQUEST = True to cause the session to be modified on every request, but this is inefficient.

what does session object represent here, is it request.session ? can you please specify more details.

Correct, I am referring to request.session. I updated the ticket description and added more detail.

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #32088 – Description

    initial v2  
    1 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 object. This makes it complicated to implement sliding expiration sessions if the sessions are not being modified by some other means.
     1For 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`) object. This makes it complicated to implement sliding expiration sessions if the sessions are not being modified by some other means.
    22
    33Of course, we could set `SESSION_SAVE_EVERY_REQUEST = True` to cause the session to be modified on every request, but this is inefficient.
     4
     5Currently, 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)
     6
     7{{{#!python
     8>>> from django.contrib.sessions.models import Session
     9>>> s = Session.objects.get(pk='2b1189a188b44ad18c35e113ac6ceead')
     10>>> s.expire_date
     11datetime.datetime(2005, 8, 20, 13, 35, 12)
     12}}}
     13
     14This 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.
Back to Top