Python 灵巧类型中的反向引用关系列表
我创建了两种灵巧类型:lab_equipment.py和class_activity.py。 class_活动类型包含与lab_活动类型的以下关系: class_activity.py:Python 灵巧类型中的反向引用关系列表,python,plone,dexterity,Python,Plone,Dexterity,我创建了两种灵巧类型:lab_equipment.py和class_activity.py。 class_活动类型包含与lab_活动类型的以下关系: class_activity.py: class IClassActivity(form.Schema, IImageScaleTraversable): [...] dexteritytextindexer.searchable('apparatus') apparatus = RelationList( titl
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_设备页面模板中列出class_活动类型中的相关成员
有没有办法将RelationList从class_活动类型反向引用到lab_活动类型,然后将此列表显示到页面模板中?要检索回引用(使用指定属性指向特定对象的所有对象),您不能简单地使用from_对象或from_路径,因为源对象存储在关系中,没有采集包装器。您应该使用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)中复制了答案,我不久前发布了:谢谢!这正是我要找的!这已经工作了一段时间,但现在我在加载某些内容类型时出现错误:
[…]模块z3c.relationfield.relationfield,第28行,在from_id模块plone.app.relationfield.monkey中,第20行,在get_from_对象模块5.intid.intid中,第44行,在寄存器模块zope.component.hooks中,第104行,在适配器钩子模块zope.security.adapter的第88行中,在模块5.intid.keyreference的第58行中,在初始化中: