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?
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.