简体   繁体   English

如何获得一个RelationList来按object_provides进行过滤?

[英]How do I get a RelationList to filter by object_provides?

I have having some trouble getting a RelationList to display what I want in the widget. 我在获取一个RelationList来显示我想要的小部件时遇到一些麻烦。 The behavior I've seen doesn't make sense to me: /mysite/folder1/foo - new content, where I want to create a relation /mysite/folder2/bar - the item I want to link to. 我所看到的行为对我来说没有意义:/ mysite / folder1 / foo-新内容,我要在其中创建关系/ mysite / folder2 / bar-我要链接的项目。

source=ObjPathSourceBinder(object_provides='foo.bar.IMyInterface')

This seems to display all Dexterity content regardless of interface - although only content that provides IMyInterface is selectable. 尽管仅选择提供IMyInterface的内容,但无论界面如何,这似乎都能显示所有敏捷内容。

source=ObjPathSourceBinder(navigation_tree_query={'object_provides':'foo.bar.IMyInterface'}))

I don't understand why this results in different behavior, but it results in no content being shown. 我不明白为什么这会导致不同的行为,但是却导致没有内容显示。 It appears to be an issue with the query path, because this does work: 查询路径似乎有问题,因为它确实起作用:

source=ObjPathSourceBinder(navigation_tree_query={'path':{'query':'/mysite/folder2'},'object_provides':'foo.bar.IMyInterface'}))

However that's not ideal because it requires knowing the path. 但是,这并不理想,因为它需要知道路径。 I was expecting the behavior to show only folderish content and content that provides my interface, with only content that provides my interface selectable. 我期望该行为仅显示类似文件夹的内容和提供我的界面的内容,而仅提供提供我的界面可选的内容。 Is that not possible? 那不可能吗?

I am aware that relations are no longer supported by default https://pypi.python.org/pypi/plone.app.dexterity#relation-support-no-longer-included-by-default (I'm using plone.app.dexterity 1.2.1 and Plone 4.2) 我知道默认情况下不再支持关系https://pypi.python.org/pypi/plone.app.dexterity#relation-support-no-longer-included-default-default (我正在使用plone.app .dexterity 1.2.1和Plone 4.2)

Alternatively, a simple select widget would be fine, but I was not able to get any default z3c.form widgets to work correctly with this field (they rendered fine, but had no selectable content). 另外,一个简单的select小部件就可以了,但是我无法获得任何默认的z3c.form小部件来与该字段正确配合使用(它们呈现的很好,但是没有可选内容)。 Perhaps it would be better to just use a regular z3c schema list with a vocabulary where the value is the UID, and look up the object myself? 也许仅将常规z3c模式列表与词汇表(其中值是UID)一起使用,然后自己查找对象会更好?

I'm not sure what I was doing wrong before, but I got the following to work for a simple select box (ordered): from five import grok from plone.directives import form from Products.CMFCore.utils import getToolByName from z3c.form.browser.orderedselect import OrderedSelectFieldWidget from z3c.relationfield.schema import RelationChoice, Relation, RelationList from zope.schema.interfaces import IContextSourceBinder from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm 我不确定之前做错了什么,但是我得到了下面的内容来处理一个简单的选择框(有序):从plone.directives的五个导入grok.directives从Products.CMFCore.utils的导入表单,从z3c.form的getToolByName导入.browser.orderedselect从z3c.relationfield.schema导入OrderedSelectFieldWidget导入zope.schema.interfaces的RelationChoice,Relation,RelationList从zope.schema.vocabulary导入IContextSourceBinder导入SimpleVocabulary,SimpleTerm

@grok.provider(IContextSourceBinder)
def possibleVals(context):
  catalog = getToolByName(context,'portal_catalog')
  brains = catalog(object_provides='foo.bar.IMyInterface')
  return SimpleVocabulary([SimpleTerm(value=b.getObject(),token=b.getPath(),title=b.Title) for b in brains])

...

form.widget(myfield=OrderedSelectFieldWidget)
myfield = RelationList(
    title=_(u'My field'),
    required=False,
    value_type=RelationChoice(title=_(u'My field'),
                              source=possibleVals),
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM