首页 > 解决方案 > 在 plone 中另一个项目的选择字段中列出创建项目的标题

问题描述

我的 Plone 项目有问题,我无法解决。'Car' 应该创建一个包含所有 'Color' 实例的列表。所有“颜色”实例都在给定的容器中。我不能让它静态,因为我想在将来添加更多的“颜色”实例。我尝试选择容器中的每个项目并将其添加到我的词汇表中。我只需要我的对象的 id/title,但我总是会遇到大量的失败堆栈跟踪。最后,我想在创建类似于下拉列表的新“汽车”实例时从给定实例中选择一种颜色。我已阅读文档但找不到解决方案,这是我最好的主意。我也不是 python 程序员,这是我的第一个 plone 项目。如果您需要,我可以稍后添加完整的失败列表。

我感谢每一点帮助。谢谢你。

 ```colour= schema.Choice(
       title=u"Colour",
       description=u"Paintjob",
        vocabulary=givecolour(),
        required=False
    )

    @provider(IContextSourceBinder)
        def givecolour():
        colourlist = self.context.portal_catalog(path={"query" : "/madb-entw/it/colourcontainer", "depth" : 1})
        list = []
        i = 0

        for colour in colourlist:
            list.append(
                    SimpleVocabulary.createTerm(
                        colourlist.[i].getObject().id
                    )
            )
            i += 1

        return SimpleVocabulary(list)```

标签: pythonplone

解决方案


请始终添加您的痕迹,以便我们更好地帮助您。还有官方的community.plone.org 论坛,那里有更多的人可以帮助你。

我建议您使用plone.api来查找您的对象,这更容易并且很好证明。

像这样的东西:

from plone import api
color_brains = api.content.find(context=api.content.get(path='/madb-entw/it/colourcontainer'), depth=1, portal_type='Color')
# no need to do getOject() here, get the id/title directly from the catalog brain
colors = [(color.id, color.Title) for color in color_brains]

对您的查询的一个注释:

colourlist = self.context.portal_catalog(path={"query" : "/madb-entw/it/colourcontainer", "depth" : 1})

路径必须是绝对的,这意味着它包括 Plone 站点 ID,并且在另一个 Plone 站点中可能会有所不同。所以绝对路径在这里不是一个好主意,最好获取门户对象并从那里遍历你的相对路径。如果 madb-entw 是您的 Plone 站点 ID:

portal.restrictedTraverse('it/colourcontainer')

或者更好的是,使用 plone.api.content.get(path='/it/colourcontainer') 更清洁和更容易。


推荐阅读