KindError:属性r必须是SecondModel的实例,为什么?

时间:2010-06-09 07:32:05

标签: python google-app-engine model properties

class FirstModel(db.Model):
    p = db.StringProperty()
    r=db.ReferenceProperty(SecondModel)

class SecondModel(db.Model):
    r = db.ReferenceProperty(FirstModel)

class sss(webapp.RequestHandler):
  def get(self):
    a=FirstModel()
    a.p='sss'
    a.put()
    b=SecondModel()
    b.r=a
    b.put()

    a.r=b
    a.put()
    self.response.out.write(str(b.r.p))

错误是:

Traceback (most recent call last):
  File "D:\Program Files\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 511, in __call__
    handler.get(*groups)
  File "D:\zjm_code\helloworld\a.py", line 158, in get
    a.r=b
  File "D:\Program Files\Google\google_appengine\google\appengine\ext\db\__init__.py", line 3009, in __set__
    value = self.validate(value)
  File "D:\Program Files\Google\google_appengine\google\appengine\ext\db\__init__.py", line 3048, in validate
    (self.name, self.reference_class.kind()))
KindError: Property r must be an instance of SecondModel

感谢

2 个答案:

答案 0 :(得分:0)

您显示的代码甚至不应该编译 - 您无法使用尚未定义的类实例化引用属性 - 除非您在某个地方有另一个未定义的SecondModel定义,在这种情况下问题是FirstModel有一个对原始SecondModel的引用,但是你传递了一个新的实例,你用它覆盖那个。

答案 1 :(得分:-2)

现在好了:

class SecondModel(db.Model):
    pass

class FirstModel(db.Model):
    p = db.StringProperty(choices=set(["aa", "bb", "cc"]))
    r=db.ReferenceProperty(SecondModel)

class SecondModel(db.Model):
    r = db.ReferenceProperty(FirstModel)
    s=db.StringProperty()

class sss(webapp.RequestHandler):
  def get(self):
    #'''
    a=FirstModel()
    a.p='cc'
    a.put()
    b=SecondModel()
    b.r=a
    b.s='kkk'
    b.put()

    a.r=b.key()
    a.put()
    #'''
    #a=FirstModel.all().filter('p =','cc').get()
    #b=a.r
    #self.response.out.write(a.secondmodel_set.filter('r = ', a).get().s)
    self.response.out.write(b.r.p+'<br/>'+a.r.s)