[英]Listing the title of created items in a choice field of another item in plone
I have a problem with my Plone item I cannot solve.我的 Plone 项目有问题,我无法解决。 'Car' is supposed to create a list of all instances of 'Colour'. 'Car' 应该创建一个包含所有 'Color' 实例的列表。 All 'Colour' instances are in a given container.所有“颜色”实例都在给定的容器中。 I cannot make it static because I want to add more 'Colour' instances in the future.我不能使它成为 static 因为我想在将来添加更多的“颜色”实例。 I tried selecting each item in my container and add it to my vocabularylist.我尝试选择容器中的每个项目并将其添加到我的词汇表中。 I only need the id/title of my object, but I always end up with a giant stacktrace of failures.我只需要我的 object 的 id/title,但我总是会遇到大量的失败堆栈跟踪。 In the end I want to choose a colour out of the given instances on creating a new 'Car' instance similar to a dropdown.最后,我想在创建类似于下拉列表的新“汽车”实例时从给定实例中选择一种颜色。 I have read the docs but cannot find a solution and this is my best idea.我已阅读文档但找不到解决方案,这是我最好的主意。 I am also not a python programmer and this is my first plone project.我也不是 python 程序员,这是我的第一个 plone 项目。 I can add the complete failure list later if you need it.如果您需要,我可以稍后添加完整的失败列表。
I appreciate every bit of help.我感谢每一点帮助。 Thank you.谢谢你。
```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)```
Please always add your traces, so that we can help you better.请始终添加您的痕迹,以便我们更好地帮助您。 There is also the official community.plone.org forum, where are more people can help you.还有官方的community.plone.org 论坛,那里有更多的人可以帮助你。
I recommend you to use the plone.api to find your objects, this is a bit easier and well doumented.我建议您使用plone.api来查找您的对象,这更容易并且很好证明。
something like this:像这样的东西:
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]
One note to your query:对您的查询的一个注释:
colourlist = self.context.portal_catalog(path={"query": "/madb-entw/it/colourcontainer", "depth": 1}) colourlist = self.context.portal_catalog(path={"query": "/madb-entw/it/colourcontainer", "depth": 1})
Path has to be absolute, which means it includes the Plone site id and this can be different in another Plone site.路径必须是绝对的,这意味着它包括 Plone 站点 ID,并且在另一个 Plone 站点中可能会有所不同。 So an absolute path is not a good idea here, better get the portal object and traverse your path relative from there.所以绝对路径在这里不是一个好主意,最好获取门户 object 并从那里遍历您的相对路径。 If madb-entw is your Plone site id:如果 madb-entw 是您的 Plone 站点 ID:
portal.restrictedTraverse('it/colourcontainer')
or better as above, use plone.api.content.get(path='/it/colourcontainer') Which is cleaner and easier.或者更好,使用 plone.api.content.get(path='/it/colourcontainer') 更清洁和更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.