使用 Zope 对象唯一 ID ( _p_oid ) 访问对象本身
每个 Zope 对象都有它自己的唯一 ID ( _p_oid )。
要将其转换为整数值:
from Shared.DC.xml.ppml import u64 as decodeObjectId
oid = decodeObjectId(getattr(<Object instance>, '_p_oid'))
是否可以获取具有 _p_oid 的对象本身?
我尝试了以下方法:
from ZODB.utils import p64
object = <RootObject instance>._p_jar[p64(oid)]
但这似乎是错误的方法,因为获取对象后我无法更改任何属性和对象。 Absolute_url() 返回空字符串。
Every Zope object has it's own unique id ( _p_oid ).
To convert it into integer value:
from Shared.DC.xml.ppml import u64 as decodeObjectId
oid = decodeObjectId(getattr(<Object instance>, '_p_oid'))
Is it possible to get object itself having it's _p_oid?
I tried this:
from ZODB.utils import p64
object = <RootObject instance>._p_jar[p64(oid)]
But it seems it's a wrong way because after getting object I can't change any property and object.absolute_url() returns empty string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只要您尝试加载的对象的类在 Python 环境中可用,并且只要您的 oid 不是来自安装在根目录中某处的另一个数据库,这就应该有效。
您能描述一下这对您不起作用的方式吗?
看看以下是否有效(它应该获取根对象,其中 _p_oid == 0):
This should work, as long as the class of the object you're trying to load is available in the Python environment, and as long as your oid isn't from another database mounted somewhere within the root.
Can you describe the way in which this is failing to work for you?
See whether the following works (it should get the root object, which has _p_oid == 0):
您可以通过这种方式很好地访问该对象,但您会得到一个未包装的对象。
在 Zope 中,通常通过遍历检索对象,并且通过这种方式检索的每个下一个对象都包装在正确的获取上下文中。此上下文告诉每个对象它的父对象是什么,这又用于计算对象的绝对 URL 及其安全上下文。
您最好使用 Zope intid 工具(通过它的 Five.intid 集成层);这为每个对象提供了一个唯一的整数 ID,该实用程序不仅跟踪该对象,还跟踪其路径,因此您可以使用正确的上下文取回该对象。
You can access the object just fine that way, but you get an unwrapped object.
In Zope, the object is normally retrieved via traversal, and every next object you retrieve this way is wrapped in the correct acquisition context. This context tells every object what it's parent object is, and this is in turn used to calculate the object's absolute URL and it's security context.
You would be better off using the Zope intid facilities (via it's five.intid integration layer); that gives you a unique integer ID for each object, and the utility not only keeps track of the object but also of it's path, so you can get the object back with the correct context.
据我所知,正确的方法是使用连接实例的
get
方法:编辑:似乎
dbroot._p_jar
是一个ZODB.Connection.Connection
对象就像 db.open() 的返回类型一样,因此也许可以假设这两种方式是等效的。可以说,conn.get(...) 看起来更干净,因为它不涉及访问以下划线前缀的方法。As far as I know, the correct way to do it is to use the
get
method of the connection instance:EDIT: it seems that
dbroot._p_jar
is anZODB.Connection.Connection
object just like the return type ofdb.open()
so perhaps it can be assumed that both ways are equivalent. Arguably,conn.get(...)
seems cleaner as it does not involve accessing underscore-prefixed methods.