ndb获取& amp; get_or_insert如何使用? (总是引发异常)

ndb获取& amp; get_or_insert如何使用? (总是引发异常)

问题描述:

我写下面的代码

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()

我写的地方

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

sdk加薪

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

当我致电KV.get()

when I call KV.get ()

sdk加薪

在获得的文件"/home/bitcoin/42btc/zapp/_plugin/auth/model/gae/user.py"中,第14行

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

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

...

BadRequestError:缺少密钥ID/名称

BadRequestError: missing key id/name

那么,如何使用NDB?

So , how to use NDB?

get_or_insert()方法采用的字符串仅是密钥的ID部分,而不是Key.它不能使用数字ID.

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.