views:

65

answers:

2
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))

the error is :

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

thanks

A: 

The code you show shouldn't even compile - you can't instantiate a reference property with a class that isn't yet defined - unless you have another definition of SecondModel somewhere that you haven't included, in which case the issue is that FirstModel has a reference to the original SecondModel, but you're passing it an instance of the new one you overwrote that one with.

Nick Johnson
A: 

it is ok now :

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)
zjm1126
You can't reference `SecondModel` before it's defined. If you're not getting an error from the above code, you're defining SecondModel twice, and things will almost certainly break somehow.
Wooble
i updated it , now it is ok
zjm1126