我已经创建了两种灵活类型: 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类型,然后将该列表显示到页面模板中?
发布于 2012-04-27 14:00:27
要检索反向引用(所有使用指定属性指向特定对象的对象),您不能简单地使用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')附言:我从我自己的灵巧问题#234中抄袭了答案,我前段时间发布了:http://code.google.com/p/dexterity/issues/detail?id=234&colspec=ID%20Type%20Status%20Priority%20Difficulty%20Milestone%20Owner%20Summary
https://stackoverflow.com/questions/10336705
复制相似问题