Plone 4 相关项目回溯参考问题

发布于 2024-11-07 06:26:17 字数 786 浏览 1 评论 0原文

我未能找到相关项目的反向引用对象。

我的代码:

back_rels = list(catalog.findRelations({'to_id': intids.getId(aq_base(self.context))}))

for rel in back_rels:
    ob = portal.unrestrictedTraverse(rel.from_path)

在 ob = Portal.unrestrictedTraverse(rel.from_path) 运行时抛出异常。

调试结果:

> len(back_rels)
> 1 
> rel
> <z3c.relationfield.relation.RelationValue object at oxoA86f8f0>
> rel.from_path 
> 'new-grants-target-bioterrorism'
> rel.to_path
> '/portal/urnews/ur-gets-20-million-for-biodefense-studies'

我猜问题是 rel.from_path 不像 rel.to_path 那样返回完整路径。

我的问题是 rel.from_path 如何返回完整路径并在

portal.unrestrictedTraverse(rel.from_path)?

我运行 Plone 4 并使用灵巧内容类型时获取正确的对象。

I failed to find back reference object for related items.

my code:

back_rels = list(catalog.findRelations({'to_id': intids.getId(aq_base(self.context))}))

for rel in back_rels:
    ob = portal.unrestrictedTraverse(rel.from_path)

It throws exception when running at ob = portal.unrestrictedTraverse(rel.from_path).

Debug results:

> len(back_rels)
> 1 
> rel
> <z3c.relationfield.relation.RelationValue object at oxoA86f8f0>
> rel.from_path 
> 'new-grants-target-bioterrorism'
> rel.to_path
> '/portal/urnews/ur-gets-20-million-for-biodefense-studies'

I guess the problem is the rel.from_path doesn't return the full path like the rel.to_path does.

My question is how can rel.from_path return with full path and get right object at

portal.unrestrictedTraverse(rel.from_path)?

I am running Plone 4 and use dexterity content type.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世俗缘 2024-11-14 06:26:17

不幸的是你不能直接访问 from_object 。本期对此进行了解释 http://code.google.com/p /dexterity/issues/detail?id=95

使用 from_id,它存储 IntId 并使用 IntIds 实用程序检索特定对象:

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

Unfortunately you can't access from_object directy. It is explained in this issue http://code.google.com/p/dexterity/issues/detail?id=95

Use from_id, which stores IntId and use IntIds utility to retrieve the particular object:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文