Monday, June 29, 2009

Store dict in AppEngine Datastore

This post is about Python and Google Appengine.

I want to store a dict/dictionary in a model. There is no native DictProperty. However there are workarounds.
  • Encode it somehow (for example with pickle) and store it as a blob.
  • Use an Expando model and store your key-values with setattr(yourmodel, key, value). You can get your dict later with yourmodel.dynamic_properties(). See also delattr. This seems to trigger encoding bugs for non-ascii keys.
  • Store keys and values separately with two StringListProperty. Combine them to build a dict with dict(zip(keylist, valuelist)).

1 comment:

thesweeheng said...

We can write a custom Property class to pickle (and compress) any serializable Python object (dictionaries included).

Here's an example: