|
Re: odd behavior with session variable that contains a list: msg#02320DjangoUsers
Makes perfect sense, thanks for that pointer! On Jul 31, 11:17 am, Alex Gaynor <alex.gay...@xxxxxxxxx> wrote: > On Fri, Jul 31, 2009 at 1:15 PM, Margie<margierogin...@xxxxxxxxx> wrote: > > > I am seeing some behavior with session that I don't understand. If I > > have a session variable called recentAddIds that contains a list. I > > find that if I append to that list, like this: > > > request.session['recentAddIds'].append(newlySavedId) > > > Then when I next receive a GET, I find that request.session > > ['recentAddIds'] does not contain the appended data (but does contain > > the data that was there prior to the append). > > > If I instead make a copy of the list and put that copy into the > > session variable, then when I next receive a GET, my session variable > > does not contain the newly appended data. > > > request.session['recentAddIds'] = [x for x in request.session > > ['recentAddIds']] + [newlySavedId] > > > Can anyone explain why this is and if it is expected behavior? Is it > > ok for sesion variables to contain lists or other complex structures, > > or should they just be simple strings? > > > Margie > > The reason this is is because of how some of python's magic methods > work. doing request.SESSION['key'].append() uses the __getitem__ > method on request.SESSION and calls append on the result, whereas > doing request.SESSION['key'] = val uses the __setitem__ > method.http://docs.djangoproject.com/en/dev/topics/http/sessions/#when-sessi... > describes how to work with this behavior. > > Alex > > -- > "I disapprove of what you say, but I will defend to the death your > right to say it." -- Voltaire > "The people's good is the highest law." -- Cicero > "Code can always be simpler than you think, but never as simple as you > want" -- Me --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@xxxxxxxxxxxxxxxx To unsubscribe from this group, send email to django-users+unsubscribe@xxxxxxxxxxxxxxxx For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---
|
|
||||||||||||||||||||||||||
|
|
|
| News | Mail Home | sitemap | FAQ | advertise |