如何在google appengine上制作持久的python字典

fdb*_*fdb 2 python persistence dictionary

使用appengine的数据存储框架,什么是使持久性{}的pythonic方法?

haw*_*ett 5

如果要查询单个字典元素,则只需要使用expando选项.

假设您不想这样做,那么您可以使用自定义属性 -

class ObjectProperty(db.Property):
  data_type = db.Blob

  def get_value_for_datastore(self, model_instance):
    value = self.__get__(model_instance, model_instance.__class__)
    pickled_val = pickle.dumps(value)
    if value is not None: return db.Blob(pickled_val)

  def make_value_from_datastore(self, value):
    if value is not None: return pickle.loads(str(value))

  def default_value(self):
    return copy.copy(self.default)
Run Code Online (Sandbox Code Playgroud)

请注意,上面的属性def我从Nick Johnson生成的一些代码中获得.它是git hub上的一个项目,包含许多其他自定义属性.