[Zope] Persisting a dictionary as a object property

Martijn Pieters mj@digicool.com
Thu, 6 Jul 2000 02:21:33 +0200


On Wed, Jul 05, 2000 at 04:52:44PM -0700, Andy McKay wrote:
> Ok so I have an object, I have many properties persisting on the object
> using the wonderful PropertyManager. But I want to persist a dictionary.
> This isn't an option for the PropertyManager. I added a dictionary into the
> object just by adding the line: _map = {}

You will have to let the persistance machinery know that you changed the
dictionary. A dictionary is a mutable object, and persistance can only track
inmutable objects automatically. See the ZODB docs:

  http://www.zope.org/Documentation/Developer/Models/ZODB/ZODB_Persistent_Objects_Persistent_Doc.html

(http://www.zope.org/Documentation/Developer/Models/ZODB for the framed set,
choose Persitent Objects, then Persitent in the bottom-left frame).

You should either treat the dictionary as immutable:

  data = self._map
  data.append(foo)
  self._map = data

or set a special flag to let the ZODB know the objetc has changed:

  self._map.append(foo)
  self._p_changed = 1

You could also replace the dictionary by a PersitentMapping instance. See
above docs for more info.

-- 
Martijn Pieters
| Software Engineer            mailto:mj@digicool.com
| Digital Creations          http://www.digicool.com/
| Creators of Zope               http://www.zope.org/
| ZopeStudio: http://www.zope.org/Products/ZopeStudio
-----------------------------------------------------