我创建了两种敏捷类型:lab_equipment.py,class_activity.py。 class_activity类型包含与lab_activity类型的以下关系:
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'}},
),
),
)
[...]
现在我需要在lab_equipment页面模板中列出class_activity类型的相关成员。
有没有办法将RelationList从class_activity类型反向引用到lab_activity类型,然后将此列表显示到页面模板中?
答案 0 :(得分:3)
要检索反向引用(使用指定属性指向特定对象的所有对象),您不能简单地使用from_object或from_path,因为源对象存储在没有获取包装器的关系中。您应该使用from_id和helper方法,它们搜索IntId目录中的对象。
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
请注意,此方法不会检查有效期限和到期日期或内容语言。
在您的情况下,您需要从实验室设备浏览器视图的某种方法调用此方法,并将反向引用对象的列表传递给模板。例如:
class LabEquipmentView(BrowserView):
def aparatus_backrefs(self):
return back_references(self.context, 'apparatus')
P.S。我复制了我自己的Dexterity问题#234我在前一段时间发布的答案:http://code.google.com/p/dexterity/issues/detail?id=234&colspec=ID%20Type%20Status%20Priority%20Difficulty%20Milestone%20Owner%20Summary