简体   繁体   中英

Back references with z3c.relationfield in Dexterity

With z3c.relationfield.schema.RelationList or RelationChoice it is possible to maintain relationships to other Dexterity content objects. In Archetypes we had a functionality context. getBRefs() in order to retrieve a list of objects referencing the current 'context' object. Is there something similar in z3c.relationfield or in Dexterity? What is the canonical way to get hold of the "back references" in Dexterity?

The following code taken from

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

is working:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM