[英]How to decode a Google App Engine entity Key path str in Python?
In Google App Engine, an entity has a Key. 在Google App Engine中,实体具有密钥。 A key can be made from a path, in which case str(key) is an opaque hex string.
可以从路径创建密钥,在这种情况下,str(密钥)是不透明的十六进制字符串。 Example:
例:
from google.appengine.ext import db
foo = db.Key.from_path(u'foo', u'bar', _app=u'baz')
print foo
gives 给
agNiYXpyDAsSA2ZvbyIDYmFyDA
if you set up the right paths to run the code. 如果您设置正确的路径来运行代码。
So, how can one take the hex string and get the path back? 那么,如何才能获取十六进制字符串并获取路径? I thought the answer would be in Key or entity group docs, but I can't see it.
我认为答案将在Key或实体组文档中,但我看不到它。
from google.appengine.ext import db
k = db.Key('agNiYXpyDAsSA2ZvbyIDYmFyDA')
_app = k.app()
path = []
while k is not None:
path.append(k.id_or_name())
path.append(k.kind())
k = k.parent()
path.reverse()
print 'app=%r, path=%r' % (_app, path)
when run in a Development Console, this outputs: 在开发控制台中运行时,输出:
app=u'baz', path=[u'foo', u'bar']
as requested. 按照要求。 A shorter alternative is to use the (unfortunately, I believe, undocumented)
to_path
method of Key
instances: 更短的替代方法是使用
Key
实例的(不幸的是,我相信,未记录的) to_path
方法:
k = db.Key('agNiYXpyDAsSA2ZvbyIDYmFyDA')
_app = k.app()
path = k.to_path()
print 'app=%r, path=%r' % (_app, path)
with the same results. 结果相同。 But the first, longer version relies only on documented methods.
但是第一个更长的版本仅依赖于记录的方法。
Once you have the Key
object (which can be created by passing that opaque identifier to the constructor), use Key.to_path()
to get the path of a Key
as a list. 获得
Key
对象(可以通过将该不透明标识符传递给构造函数来创建)后,使用Key.to_path()
将Key
的路径作为列表获取。 For example: 例如:
from google.appengine.ext import db
opaque_id = 'agNiYXpyDAsSA2ZvbyIDYmFyDA'
path = db.Key(opaque_id).to_path()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.