ndb get & get_or_insert how to use ? (alway raise Exception)

此生再无相见时 提交于 2019-12-08 06:46:09

问题


I write code as below

from google.appengine.ext import ndb

__metaclass__ = type


class UserSession(ndb.Model):
    session = ndb.BlobProperty()


class KV:
    @staticmethod
    def get(id):
        r = ndb.Key(UserSession, int(id)).get()
        if r:
            return r.session

    @staticmethod
    def set(id, value):
        return UserSession.get_or_insert(int(id), session=value)

    @staticmethod
    def delete(id):
        ndb.Key(UserSession, int(id)).delete()

where I write

id = 1
key = ndb.Key(UserSession, int(id))
UserSession.get_or_insert(key, session=1)

the sdk raise

TypeError: name must be a string; received Key('UserSession', 1)

when I call KV.get ()

the sdk raise

File "/home/bitcoin/42btc/zapp/_plugin/auth/model/gae/user.py", line 14, in get

    r = ndb.Key(UserSession,int(id)).get()

...

BadRequestError: missing key id/name

So , how to use NDB?


回答1:


The get_or_insert() method takes a string which is only the ID part of the key, not a Key. It cannot use numeric IDs.



来源:https://stackoverflow.com/questions/15179240/ndb-get-get-or-insert-how-to-use-alway-raise-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!