简体   繁体   English

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

[英]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.

相关问题 plone - 在编辑表单中,什么可能导致门户目录在自动完成选择字段的源对象中失败? - plone - While in an edit form, what can cause portal catalog to fail in a source object for an autocomplete choice field? 如何使用 python ebaysdk 从多变体列表中过滤项目(多个项目的列表) - How to use python ebaysdk to filter items from a multi variation listing (listing of more than one item) 根据 Django 中另一个字段中选择的选项动态更新字段的值 - Dynamically updating values of a field depending on the choice selected in another field in Django 如何根据django admin中的另一个选项字段限制选择字段选项 - How to limit choice field options based on another choice field in django admin 创建项目后如何创建日期字段? - How to create a date field once item is created? 如何定义模型的一个字段以从另一个应用程序的另一个模型的另一个字段中进行选择? - How to define a field of a model to have a choice from another field of another model of another app? 来自另一个模型实例的Django模型选择字段 - Django model choice field from another model instance 如何搜索特定字段包含一个列表中的所有项目但不包含另一个列表中的任何项目的文档? - How to search for documents whose specific field contains all items from one list and does not contain any item from another list? python:用另一个列表中的项目替换列表中的项目 - python: replace item in a list with items in a another list 按项目所属的类别列出项目 - Listing items by the category they belong to
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM