我正在浏览包含内容项的文件夹。我使用portal_catalog来获取在某些路径上搜索到的大脑。大脑可以访问元数据,而brain.getObject()将返回实际的对象。我用brain.getObject()。aq_parent得到了一个对象的父对象。现在我想获得对象在父对象中的位置。起初我尝试了brain.getObject()。getObjPositionInParent(),之后,我意识到getObjPositionInParent()是一个可以从索引数据访问的属性。
idxData = catalog.getIndexDataForRID(brain.getRID())
sJson = json.dumps( idxData )
l = brain.getObject()
lUpdate = {'path': '/'.join( l.getPhysicalPath()), 'meta_type': l.meta_type, 'title':l.getRawTitle(), 'remoteUrl': l.getRemoteUrl(), 'json':sJson}
当我将其打印到屏幕上时,我看到了从catalog.getIndexDataForRID调用返回的dict中的所有项目。问题是对于所有对象,getObjPositionInParent()是一个空数组([])。在此页http://developer.plone.org/searching_and_indexing/query.html上,似乎值应为整数。这让我想知道是否必须创建索引数据,如果是这样的话,那么我可能会从对象到达太远以获取必须已存在的数据(因为文件夹显然知道放置每个孩子的位置在)。获取内容对象在父级中的位置的最佳方法是什么?提前感谢您提供任何信息?
更多:
我不确定为什么找不到适配器,但它可能与缺少注册它有关。这是一个脚本,我构建Zope环境直接从文件读取ZODB,而不是在运行的Zope实例之上。我是否有可能使用GlobalSiteManager注册适配器?
谢谢你,Mathias。当我使用sort_on =“getObjPositionInParent”时,我收到以下错误:
Traceback (most recent call last):
File "extractMenuStructure.py", line 459, in <module>
res = processFolder( home['childItems'], '/Sanford Guide Web Edition/' + appFolderNm + '', config['screens'] )
File "extractMenuStructure.py", line 390, in processFolder
results = portal_catalog(path={"query":currentPath, "depth":d},sort_on="getObjPositionInParent")
File "/Applications/Plone/buildout-cache/eggs/Products.CMFPlone-4.1.2-py2.6.egg/Products/CMFPlone/CatalogTool.py", line 427, in searchResults
return ZCatalog.searchResults(self, REQUEST, **kw)
File "/Applications/Plone/buildout-cache/eggs/Products.ZCatalog-2.13.20-py2.6.egg/Products/ZCatalog/ZCatalog.py", line 604, in searchResults
return self._catalog.searchResults(REQUEST, used, **kw)
File "/Applications/Plone/buildout-cache/eggs/Products.ZCatalog-2.13.20-py2.6.egg/Products/ZCatalog/Catalog.py", line 909, in searchResults
return self.search(args, sort_index, reverse, sort_limit, _merge)
File "/Applications/Plone/buildout-cache/eggs/Products.ZCatalog-2.13.20-py2.6.egg/Products/ZCatalog/Catalog.py", line 658, in search
b_size=b_size)
File "/Applications/Plone/buildout-cache/eggs/Products.ZCatalog-2.13.20-py2.6.egg/Products/ZCatalog/Catalog.py", line 678, in sortResults
index_key_map = sort_index.documentToKeyMap()
File "/Applications/Plone/buildout-cache/eggs/plone.app.folder-1.0.4-py2.6.egg/plone/app/folder/nogopip.py", line 91, in documentToKeyMap
ids = folder.getOrdering().idsInOrder()
File "/Applications/Plone/buildout-cache/eggs/plone.folder-1.0.1-py2.6.egg/plone/folder/ordered.py", line 41, in getOrdering
adapter = getAdapter(self, IOrdering)
File "/Applications/Plone/buildout-cache/eggs/zope.component-3.9.5-py2.6.egg/zope/component/_api.py", line 96, in getAdapter
raise ComponentLookupError(object, interface, name)
zope.component.interfaces.ComponentLookupError: (<ATFolder at /Sanford Guide Web Edition/amt>, <InterfaceClass plone.folder.interfaces.IOrdering>, u'')
答案 0 :(得分:5)
最好的方法就是做索引本身。
基于目录工具的代码段(Products.CMFP lone)
from Acquisition import aq_inner
from Acquisition import aq_parent
from OFS.interfaces import IOrderedContainer
obj = brain.getObject()
parent = aq_parent(aq_inner(obj))
ordered = IOrderedContainer(parent, None)
if ordered is not None:
return ordered.getObjectPosition(obj.getId())
return 0