ndb get & get_or_insert how to use ? (alway raise

2019-05-31 16:13发布

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条回答
劳资没心,怎么记你
2楼-- · 2019-05-31 17:02

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.

查看更多
登录 后发表回答