1

私は以下のようにコードを書きます

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() を呼び出すと

SDKレイズ

ファイル "/home/bitcoin/42btc/zapp/_plugin/auth/model/gae/user.py"、14 行目、get

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

...

BadRequestError: キー ID/名前がありません

では、NDB の使用方法は?

4

1 に答える 1

6

get_or_insert() メソッドは、キーではなく、キーの ID 部分のみである文字列を取ります。数値 ID は使用できません。

于 2013-03-03T16:43:32.753 に答える