建模实体关系在s之后返回None

2024-10-01 04:43:56 发布

您现在位置:Python中文网/ 问答频道 /正文

在阅读了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”上,对象没有引用的数据。为什么?我如何使上述工作?你知道吗


Tags: 数据fromimporttruedbmodelgooglerequired