user2665694
user2665694

Reputation:

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?

Upvotes: 2

Views: 477

Answers (1)

user2665694
user2665694

Reputation:

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

Upvotes: 8

Related Questions