Opened 4 years ago

Last modified 3 years ago

#32088 closed New feature

Django Database Sessions - Can't Retrieve expire_date from request.session (SessionStore) — at Version 4

Reported by: Nate Pinchot Owned by: nobody
Component: contrib.sessions Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by 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 (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.

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

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)

>>> from django.contrib.sessions.models import Session
>>> s = Session.objects.get(pk='2b1189a188b44ad18c35e113ac6ceead')
>>> s.expire_date
datetime.datetime(2005, 8, 20, 13, 35, 12)

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.

Change History (4)

in reply to:  description ; comment:1 by Pallav Parikh, 4 years ago

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.

in reply to:  1 comment:2 by Nate Pinchot, 4 years ago

Description: modified (diff)

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.

comment:3 by Nate Pinchot, 4 years ago

Summary: Django Database Sessions - Can't Retrieve expire_dateDjango Database Sessions - Can't Retrieve expire_date from request.session (SessionStore)

comment:4 by Nate Pinchot, 4 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top