我找不到相关项目的参考对象。
我的代码:
back_rels = list(catalog.findRelations({'to_id': intids.getId(aq_base(self.context))}))
for rel in back_rels:
ob = portal.unrestrictedTraverse(rel.from_path)
在ob = portal.unrestrictedTraverse(rel.from_path)运行时抛出异常。
调试结果:
> len(back_rels)
> 1
> rel
> <z3c.relationfield.relation.RelationValue object at oxoA86f8f0>
> rel.from_path
> 'new-grants-target-bioterrorism'
> rel.to_path
> '/portal/urnews/ur-gets-20-million-for-biodefense-studies'
我猜问题是rel.from_path没有像rel.to_path那样返回完整路径。
我的问题是rel.from_path如何以完整路径返回并在
处获得正确的对象portal.unrestrictedTraverse(rel.from_path)?
我正在运行Plone 4并使用dexterity内容类型。
答案 0 :(得分:0)
很遗憾,您无法访问from_object directy。本期http://code.google.com/p/dexterity/issues/detail?id=95
对此进行了解释使用from_id,它存储IntId并使用IntIds实用程序来检索特定对象:
from Acquisition import aq_inner
from zope.component import getUtility
from zope.intid.interfaces import IIntIds
from zope.security import checkPermission
from zc.relation.interfaces import ICatalog
def back_references(source_object, attribute_name):
""" Return back references from source object on specified attribute_name """
catalog = getUtility(ICatalog)
intids = getUtility(IIntIds)
result = []
for rel in catalog.findRelations(
dict(to_id=intids.getId(aq_inner(source_object)),
from_attribute=attribute_name)
):
obj = intids.queryObject(rel.from_id)
if obj is not None and checkPermission('zope2.View', obj):
result.append(obj)
return result