python - 在 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)```
解决方案
请始终添加您的痕迹,以便我们更好地帮助您。还有官方的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') 更清洁和更容易。
推荐阅读
- amazon-web-services - Cognito 身份池 - 将 Apple 提供商与 App 和 Web 集成
- java - 观察到动态 JSON 的 JSON 解析器异常
- javascript - 如何在 JavaScript 中使用 Python 字符串索引?
- shell - 如何选择垂直列到水平列?
- ruby-on-rails - 将 twitter bootstrap gem 从 2.x 升级到 3.x 或 4.x 会破坏 UI 样式
- javascript - 数据不显示。错误:无法获取/表达 API
- angular - 试图将事件从父级传递给子级不起作用。角 8
- node.js - /bin/sh: =: 从自定义映像运行 docker 容器时未找到
- java - 在 tomat 上部署的根映射(“/”)上的 Spring Boot 404。(轻松依赖问题)
- python - 如何修剪 keras ResNet50 预训练权重(.h5 文件)?我只有 .h5 权重文件。(我已经成功加载了 keras 权重)