Opened 4 years ago
Last modified 3 years ago
#32088 closed New feature
Django Database Sessions - Can't Retrieve expire_date — at Version 2
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 )
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
) 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 (2)
follow-up: 2 comment:1 by , 4 years ago
comment:2 by , 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 itrequest.session
?
can you please specify more details.
Correct, I am referring to request.session
. I updated the ticket description and added more detail.
Replying to Nate Pinchot:
what does
session object
represent here, is itrequest.session
?can you please specify more details.