2

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

4

1 回答 1

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
于 2012-08-09T07:18:58.740 回答