request.session['shopping_cart'] = ShoppingCart()
request.session['billing_info'] = BillingInfo()
and so on... I noticed that none of the later updates to the session
were ever remembered. After digging through the middleware code, I
realized that it sets a "modified" flag when any changes are made on
the session dictionary's keys. Well, those keys are all set when the
session is created, so changes to the sub-members didn't trigger that
modified flag.
I've been trying to think of a way of checking for any changes in the
structure will be saved, but dict's don't support hashing...
PS. As advice for the original question, my work around is to manually
set the request.session.modified when I've made a change to the
session, you could do the same to ensure the session does not expire.
They key is save the state of the session at the beginning of the
request, and then compare it at the end to see if changes were made.
You should be able to use the copy.deepcopy method to save a "pristine"
version of the session, and then just do a comparison at the end. If
different, save to the db.
So, in _get_session(self), on an AttributeError, add
self._original_session = copy.deepcopy(self._session_cache)
And then in process_reponse(), instead of if(modified), do
if(request.session._original_session != request.session._session)
Of course, the downside to doing this is doubling the data and
performing a deepcopy on every request (at least every one that uses
the session). But I figure, ram speed is much faster than db speed, so
it might not be that significant. perhaps a run-time config
SESSION_TRACK_CHANGES or something.