Remove appid (& namespace) from Key string
A string representation of entity's key always have app-id & namespace as a prefix. But in most case they are not required because too obvious.
ex> Key开发者_运维问答 : ag13YXJjbG91ZC10ZXN0chsLEgVGb3JjZRihnAEMCxIIVW5pdFNsb3QYAgw
after removing appid_namespace part : chsLEgVGb3JjZRihnAEMCxIIVW5pdFNsb3QYAgw
I know (kind,id or name) pair can be another solution for identifying unique entity but here, I just want to know how to eliminate app-id & namespace prefix and expose rest as a part of REST API...
How can I remove them?
Hey, I'm not sure how you want your REST api to work, but if you do something like
from google.appengine.ext import db
def getRESTPath (entity):
def addKeyPathRecursive (key):
restPaths.append('%s/%s' % (key.kind(), key.id_or_name()))
parentKey = key.parent()
if parentKey:
addKeyPathRecursive(parentKey)
restPaths = []
addKeyPathRecursive(entity.key())
return '/'.join(reversed(restPaths))
class Grandpa(db.Model): pass
class Papa(db.Model): pass
class Kid(db.Model): pass
kid = Kid(parent = Papa(parent = Grandpa().put()).put())
kid.put()
print getRESTPath(kid)
you get a string like Grandpa/21386/Papa/21387/Kid/21388
.
If you just want (kind, id_or_name)
without the ancestor path, I'm not sure why you're unhappy with '%s/%s' % (key.kind(), key.id_or_name())
. You can't remove namespace info from the way the datastore treats keys internally, but that doesn't mean you have to display it to users.
精彩评论