在 plone 中另一个项目的选择字段中列出已创建项目的标题



我的 Plone 项目有问题,我无法解决。"汽车"应该创建一个"颜色"的所有实例的列表。 所有"颜色"实例都在给定的容器中。我无法将其设置为静态,因为我想在将来添加更多"颜色"实例。 我尝试选择容器中的每个项目并将其添加到我的词汇表中。我只需要对象的 id/title ,但我总是以巨大的失败堆栈跟踪告终。 最后,我想从给定的实例中选择一种颜色,以创建一个类似于下拉列表的新"Car"实例。 我已经阅读了文档,但找不到解决方案,这是我最好的主意。 我也不是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)```

请始终添加您的痕迹,以便我们更好地帮助您。 还有官方 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]

查询的一条说明:

颜色列表 = self.context.portal_catalog(路径={"查询" : "/madb-entw/it/colorcontainer", "depth" : 1}(

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

portal.restrictedTraverse('it/colourcontainer')

或者更好的是,使用plone.api.content.get(path='/it/colorcontainer'( 哪个更干净,更容易。

最新更新