在Dexterity中使用z3c.relationfield返回引用

2 plone dexterity

使用z3c.relationfield.schema.RelationList或RelationChoice,可以维护与其他Dexterity内容对象的关系.在Archetypes中,我们有一个功能上下文.getBRefs()以检索引用当前"上下文"对象的对象列表.在z3c.relationfield或Dexterity中有类似的东西吗?在Dexterity中获取"反向引用"的规范方法是什么?

小智 8

以下代码取自

http://code.google.com/p/dexterity/issues/detail?id=234

工作中:

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
Run Code Online (Sandbox Code Playgroud)