简体   繁体   中英

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. 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.

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.

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)

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). 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?

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

@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),
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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