[Answered ]-How do I refer to a model defined later in a reference property in Google App Engine?

1👍

You don’t put the “B” in a string. It should be:

    class A(db.Model):
        b = db.ReferenceProperty(B)

    class B(db.Model):
        pass
👤GAEfan

1👍

Using ndb

class A(ndb.Model):
  b = ndb.KeyProperty('B')

class B(ndb.Model):
  pass

To find all instances of A, that refer to B

A.query(A.b == somekey_for_b)

This could be a method of B which is the equivalent of a db collection set but is not automagical and doesn’t have the issue of collection set name clashes

Alternately if you must stick with db then either change the order of class definition, or just use db.Model as the kind for the reference property definition rather than the actual class. It will still work, it just won’t check the specific Kind at update time.

You can validate that the correct kind is supplied by supplying your own validate method

Leave a comment