ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

敏捷类型的反向引用RelationList

2019-12-01 11:56:15  阅读:307  来源: 互联网

标签:dexterity python plone


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有