Google App Engine KeyProperty

Google App Engine KeyProperty

KeyProperty 有什么好处?如果我可以使用 user_id 和 id

连接两个实体
   class User(ndb.Model):

        name = ndb.StringProperty()

   class Attend(ndb.Model):
        user_id = ndb.IntegerProperty()
        date = ndb.DateProperty()

User(id=1,name="xyz)
Attend(user_id = 1, date = "xyz")

在 ID 上存储 KeyProperty 有几个小好处:

  • x.user_key.get() 比 ndb.Key(Attend, x.user_id).get()
  • 更容易
  • 密钥已完全指定,因此它所指的实体毫无疑问,但对于 ID,您还需要知道实体类型。

这些都是次要的,所以你可以做任何一个。我有时会根据其他因素存储密钥,有时会存储 ID。