[Zope] store dict in session

Peter Bengtsson peter at fry-it.com
Wed Jan 10 15:23:27 EST 2007


Quite possible that you have a form with something like this:

<input name="user.age:record" value="50" />
<input name="user.name:record" value="Dee" />

In python, if you do this::

def saveUser(self, user, REQUEST):
     print user # will print {'age':'50', 'name':'Dee'}
     REQUEST.SESSION.set('user_data', user) # will fail!

So you have to do this::

def saveUser(self, user, REQUEST):
     user = dict(user)
     print user # will print {'age':'50', 'name':'Dee'}
     REQUEST.SESSION.set('user_data', user) # will work!

Dennis Schulz wrote:
> According to the zope book it should be possible to store dicts in session.
> When I try to assign a dictionary to a ordinary zope 2 session, with 
> lists there is no problem. why?:
> 
>        requestlist = {}
>        for i in self.request.form:
>            key = i
>            value = self.request.form[i]
>            requestlist[i] = self.request.form[i]
>        sess = self.context.REQUEST.SESSION
>        sess['pr_reqlist'] = requestlist
> 
> I get the following error:
> 
> Traceback (innermost last):
> 
>    * Module ZPublisher.Publish, line 196, in publish_module_standard
>    * Module Products.PlacelessTranslationService.PatchStringIO, line
>      34, in new_publish
>    * Module ZPublisher.Publish, line 146, in publish
>    * Module Zope2.App.startup, line 222, in zpublisher_exception_hook
>    * Module ZPublisher.Publish, line 121, in publish
>    * Module Zope2.App.startup, line 240, in commit
>    * Module transaction._manager, line 96, in commit
>    * Module transaction._transaction, line 380, in commit
>    * Module transaction._transaction, line 378, in commit
>    * Module transaction._transaction, line 433, in _commitResources
>    * Module ZODB.Connection, line 484, in commit
>    * Module ZODB.Connection, line 526, in _commit
>    * Module ZODB.Connection, line 553, in _store_objects
>    * Module ZODB.serialize, line 407, in serialize
>    * Module ZODB.serialize, line 416, in _dump
>    * Module copy_reg, line 69, in _reduce_ex
> 
> TypeError: can't pickle instancemethod objects (Also, the following 
> error occurred while attempting to render the standard error message, 
> please see the event log for full details: An operation previously 
> failed, with traceback: File 
> &quot;C:\Python\Zope\Zope\lib\python\ZServer\PubCore\ZServerPublisher.py&quot;, 
> line 23, in __init__ response=response) File 
> &quot;C:\Python\Zope\Zope\lib\python\ZPublisher\Publish.py&quot;, line 
> 395, in publish_module environ, debug, request, response) File 
> &quot;C:\Python\Zope\Zope\lib\python\ZPublisher\Publish.py&quot;, line 
> 196, in publish_module_standard response = publish(request, module_name, 
> after_list, debug=debug) File 
> &quot;C:\Python\Zope\Instance\Products\PlacelessTranslationService\PatchStringIO.py&quot;, 
> line 34, in new_publish x = Publish.old_publish(request, module_name, 
> after_list, debug) File 
> &quot;C:\Python\Zope\Zope\lib\python\ZPublisher\Publish.py&quot;, line 
> 121, in publish transactions_manager.commit() File 
> &quot;C:\Python\Zope\Zope\lib\python\Zope2\App\startup.py&quot;, line 
> 240, in commit transaction.commit() File 
> &quot;C:\Python\Zope\Zope\lib\python\transaction\_manager.py&quot;, line 
> 96, in commit return self.get().commit(sub, deprecation_wng=False) File 
> &quot;C:\Python\Zope\Zope\lib\python\transaction\_transaction.py&quot;, 
> line 380, in commit self._saveCommitishError() # This raises! File 
> &quot;C:\Python\Zope\Zope\lib\python\transaction\_transaction.py&quot;, 
> line 378, in commit self._commitResources() File 
> &quot;C:\Python\Zope\Zope\lib\python\transaction\_transaction.py&quot;, 
> line 433, in _commitResources rm.commit(self) File 
> &quot;C:\Python\Zope\Zope\lib\python\ZODB\Connection.py&quot;, line 484, 
> in commit self._commit(transaction) File 
> &quot;C:\Python\Zope\Zope\lib\python\ZODB\Connection.py&quot;, line 526, 
> in _commit self._store_objects(ObjectWriter(obj), transaction) File 
> &quot;C:\Python\Zope\Zope\lib\python\ZODB\Connection.py&quot;, line 553, 
> in _store_objects p = writer.serialize(obj) # This calls __getstate__ of 
> obj File &quot;C:\Python\Zope\Zope\lib\python\ZODB\serialize.py&quot;, 
> line 407, in serialize return self._dump(meta, obj.__getstate__()) File 
> &quot;C:\Python\Zope\Zope\lib\python\ZODB\serialize.py&quot;, line 416, 
> in _dump self._p.dump(state) File 
> &quot;C:\Python\Zope\Python\lib\copy_reg.py&quot;, line 69, in 
> _reduce_ex raise TypeError, &quot;can't pickle %s objects&quot; % 
> base.__name__ TypeError: can't pickle instancemethod objects )
> 
> 
> _______________________________________________
> Zope maillist  -  Zope at zope.org
> http://mail.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce
> http://mail.zope.org/mailman/listinfo/zope-dev )
> 

-- 
Peter Bengtsson,
work www.fry-it.com
home www.peterbe.com
hobby www.issuetrackerproduct.com


More information about the Zope mailing list