python - Dexterity类型中的反向引用RelationList

标签 python plone dexterity

我创建了两种敏捷类型: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

请注意,此方法不检查生效日期和到期日期或内容语言。

在您的情况下,您需要从实验室设备浏览器 View 的某种方法调用此方法,并将反向引用对象列表传递给您的模板。例如:

class LabEquipmentView(BrowserView):

    def aparatus_backrefs(self):
        return back_references(self.context, 'apparatus')

附言我从我自己的 Dexterity issue #234 中复制了我之前发布的答案:http://code.google.com/p/dexterity/issues/detail?id=234&colspec=ID%20Type%20Status%20Priority%20Difficulty%20Milestone%20Owner%20Summary

关于python - Dexterity类型中的反向引用RelationList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10336705/

相关文章:

Plone 上的重定向

Plone:从 dexterity xml 模型中提取 i18n 字符串

plone - 在 Dexterity 上的字段集之间移动字段

plone - 使用新界面标记敏捷性内容类型的推荐方法

python - 在以灵活的形式设置另一个字段后,动态更改一个字段的下拉选项。架构

plone - LinguaPlone - 仅链接到可用的翻译

python - 计算从 hdf5 文件进行内存映射的大型 numpy 数组的平均值

python - 限制 google OR-Tools 上的系数值

python - 如何在图像阵列中添加 channel ?

python - "pip install Django"给出异常