How to set an ndb keyProperty

◇◆丶佛笑我妖孽 提交于 2019-12-05 19:22:42

try:

post.user_key = shuffle(users)[0].key

Maybe this helps to understand the NDB. I had the same questions with you.

class Person(ndb.Expando):
    pass

class Favourite(ndb.Expando):       
    pass

class Picture(ndb.Expando):
    pass

person  = Person()
person.put()
picture = Picture()
picture.put()
fav = Favourite(parent=person.key,
        person=person.key,
        picture=picture.key
        )
fav.put()
  1. Verify that shuffle works in this case, as User.query() returns an iter, not a list. (You can convert it to a list using shuffle( [ x for x in users ] ). Beware, this list could be looong.
  2. NDB has some really wired behavior sometimes, so id recommend you dont store an NDB-Key, but its serialized string, which is also compatible to ext.db: post.user_key = shuffle( [ x for x in users ] ).key.urlsafe()
  3. You could use KeyProperty for associations. If you need a more fine-graned control over your relations, you must implement them yourself. See https://developers.google.com/appengine/docs/python/ndb/properties#structured
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!