按照@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