ZODB equivalent of ordered dict (odict?)

I am working on PloneFormGen. PloneFormGen currently stores the entered form records internally as tuples with no corresponding column information. If new columns (form fields) are added, then the existing data becomes invalid.

This could be easily avoided by storing the data in ordered dictionaries that preserve both the entered column order and the column IDs.

Does ZODB have an ordered dictionary equivalent data type? If possible even with API mapping (Python type element manipulation and access)?


source to share

4 answers

you will probably have to create your own class as I am not aware of any current implementations.

You can find ZODB implementations preserving ordered dictates here, based on PersistentDict and OOBtree:


These implementations are based on the odict package:


Since it is not possible to persist the type of inherited dict objects to ZODB (since persistent.Persistent and dict have incompatible low-level implementations), odict provides a way to easily include different base classes (using the _dict_impl function inside everything all over the place). It is for this reason that the odict package is still used in favor of even the python 2.7 implementation it ordered or other third party order implementations.



In ZODB, you can use any ordered implementation of the dict implementation, but you will need to mark the parent object (the object that references the ordered instance instance) as modified with parent = odict_instance

each time you change it, or by setting it _p_changed

to True

. This will of course create a new permanent entry for the parent along with the ordered dict instance.

If you want an ordered dict instance to automatically detect changes, you probably have to create your own class as I am not aware of any current implementations. However, this is probably very easy to do, especially if you are using the ZODBPersistentMapping

class as a template on how to create an ordered version. You cannot use this class as a mixin unfortunately as it refers directly to the UserDict methods instead of using calls super()

( persistent.Persistent

not a new style class).

Python 2.7 has an ordered dict class in the standard library. Presumably you are still using Python 2.6 in Plone, so you will need to support it. However, if you got it back, the implementation PersistentOrderedDict

should be a direct copy from the PersistentMapping

, with all instances UserDict.IterableUserDict

replaced with your OrderedDict port.



Both werkzeug and paste provide ordered characters. You could undoubtedly reveal them for your own purposes.



If the Python object can be pickled, it can be stored in the ZODB.

Take a look at PersistantMapping , from what I understand should be enough to create a mix-in class like this:

class PersistantOrderedDict(PersistantMapping, OrderedDict):




All Articles