在阅读Python的建模实体关系(https://developers.google.com/appengine/articles/modeling)之后,我继续创建了一些示例代码:
from google.appengine.ext import ndb, db
from google.appengine.ext.db import polymodel
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
class Car(db.Model):
brand = db.StringProperty(required=True)
wheels = db.ListProperty(db.Key)
class Human(db.Model):
name = db.StringProperty(required=True)
drives = db.ReferenceProperty(reference_class=Car)
spouse = db.SelfReferenceProperty()
owns = db.ListProperty(db.Key)
class OwnedCar(db.Model):
brand = db.StringProperty(required=True)
owner = db.ReferenceProperty(Human, required=True)
paul = Human(name="Paul")
paul.put()
pauls_bmw = OwnedCar(brand="BMW", owner=paul)
pauls_bmw.put()
pauls_mercedes = OwnedCar(brand="Mercedes", owner=paul)
pauls_mercedes.put()
pauls_cars = paul.ownedcar_set
print "Paul's cars: "
for car in pauls_cars:
print "- "+car.brand
我在交互式控制台中运行了这个,令我惊讶的是(因为这本书显示了一个类似的例子),没有打印出来。
查看数据存储区,我看到数据并且它正确关联。我也可以做一个快速的GQL查询来提取数据并以上述方式打印出来(这有效),但在'insert / create'上,对象没有Reference'd数据。为什么?我如何完成上述工作?