0

我未能找到相关项目的参考对象。

我的代码:

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 并使用敏捷内容类型。

4

1 回答 1

0

不幸的是,您无法直接访问 from_object。在这个问题http://code.google.com/p/dexterity/issues/detail?id=95中有解释

使用存储 IntId 的 from_id 并使用 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
于 2011-10-02T13:56:30.650 回答