I have created two Dexterity types: lab_equipment.py, class_activity.py. The class_activity type contains the following relation to the lab_activity type:
class_activity.py:
class IClassActivity(form.Schema, IImageScaleTraversable):
[...]
dexteritytextindexer.searchable('apparatus')
apparatus = RelationList(
title=_(u"Apparatus"),
description=_(u"Choose equipment used in this activity"),
value_type=RelationChoice(
source=ObjPathSourceBinder(
object_provides=ILabEquipment.__identifier__,
navigation_tree_query= {'path': {'query':'/Plone/ug-demos/equipment'}},
),
),
)
[...]
Now I need to list related members from the class_activity type in the lab_equipment page template.
Is there a way to reverse reference the RelationList from the class_activity type into the lab_activity type and then show this list into the page template?
To retrieve back-reference (all objects pointing to particular object using specified attribute) you can't simply use from_object or from_path, because source object is stored in the relation without acquisition wrappers. You should use from_id and helper method, which search the object in the IntId catalog.
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
Please note, this method does not check effective and expiration date or content language.
In your case, you need to call this method from some method of lab equipment browser view and pass list of back referencing objects to your template. For example:
class LabEquipmentView(BrowserView):
def aparatus_backrefs(self):
return back_references(self.context, 'apparatus')
P.S. I copied the answer from my own Dexterity issue #234 I posted some time ago: http://code.google.com/p/dexterity/issues/detail?id=234&colspec=ID%20Type%20Status%20Priority%20Difficulty%20Milestone%20Owner%20Summary