敏捷类型的反向引用RelationList
作者:互联网
我创建了两种敏捷类型: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类型,然后将此列表显示到页面模板中?
解决方法:
要检索向后引用(所有使用指定属性指向特定对象的对象),您不能简单地使用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
标签:dexterity,python,plone 来源: https://codeday.me/bug/20191201/2080569.html