2011-05-13 55 views
0

我未能找回相關商品的參考對象。Plone 4相關商品返回參考問題

我的代碼:

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中得到正確的對象,並用靈巧的內容類型。

+0

問題應該在問題跟蹤報告:它是在這個問題上http://code.google.com/p/dexterity/issues/detail?id=95

使用from_id,存儲INTID和使用IntIds實用工具檢索特定對象解釋。請在dev.plone.org上報告 – toutpt 2011-05-14 15:54:23

+0

您在使用''findRelations''方法的目錄中?我無法找到它。 – marcosfromero 2011-05-14 16:52:03

回答

0

不幸的是,您無法訪問from_object directy。

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