Changes between Initial Version and Version 1 of Ticket #32088, comment 5


Ignore:
Timestamp:
Oct 11, 2020, 4:47:20 AM (4 years ago)
Author:
Pallav Parikh

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #32088, comment 5

    initial v1  
    11Replying to [ticket:32088 Nate Pinchot]:
    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.
    162
    173
     
    228> >>> request.session.expire_date
    239> datetime.datetime(2005, 8, 18, 13, 35, 12)
    24 > >>> request.session.expire_date = new_expire_date
     10> >>> request.session._expire_date = new_expire_date
    2511> >>> # only on request.session.save() new_expire_date gets stored.
    2612> >>> request.session.save()
    27 > >>> request.session.model.objects.get(session_key=request.session.session_key).expire_date
     13> >>> request.session._get_session_from_db().expire_date
    2814> datetime.datetime(2005, 8, 20, 13, 35, 12)
    2915> }}}
Back to Top