按照@mjhm和@Nick的建议,我已经将ListProperty分类为接受任何类。我有uploaded a generic version to GitHub,名为ObjectListProperty。我使用它作为使用并行ListProperty的替代方案。
ObjectListProperty在获取&放置模型时透明地序列化/反序列化。它有一个内部序列化方法,可用于简单对象,但如果它们定义了自己的序列化方法,则可以处理更复杂的对象。这里有一个简单的例子:
from object_list_property import ObjectListProperty
class Animal():
""" A simple object that we want to store with our model """
def __init__(self, species, sex):
self.species = species
self.sex = sex if sex == 'male' or sex == 'female' else 'unknown'
class Zoo(db.Model):
""" Our model contains of list of Animal's """
mammals = ObjectListProperty(Animal, indexed=False)
class AddMammalToZoo(webapp.RequestHandler):
def post(self):
# Implicit in get is deserializing the ObjectListProperty items
zoo = Zoo.all().get()
animal = Animal(species=self.request.get('species'),
sex=self.request.get('sex'))
# We can use our ObjectListProperty just like a list of object's
zoo.mammals.append(animal)
# Implicit in put is serializing the ObjectListProperty items
zoo.put()
链接被破坏,我找不到任何它曾经存在的证据:( – Thomas 2012-01-11 14:14:35
@Thomas修正了!谢谢你的抬头 – 2012-02-20 00:50:31
链接被再次破坏,但我猜这是一回事:https:/ /github.com/Willet/ObjectListProperty – 2014-05-11 11:28:50