#32088: Django Database Sessions - Can't Retrieve expire_date
----------------------------------+--------------------------------------
Reporter: Nate Pinchot | Owner: nobody
Type: New feature | Status: new
Component: contrib.sessions | Version: 3.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Old description:
> 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.
New description:
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)
{{{#!python
>>> 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.
--
Comment (by Nate Pinchot):
Replying to [comment:1 Pallav Parikh]:
> Replying to [ticket:32088 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.
--
Ticket URL: <https://code.djangoproject.com/ticket/32088#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.1cf38c3fc0f931c2c9dcfb6eff9b53de%40djangoproject.com.