如何从浏览器视图访问 z3c.form 小部件设置

发布于 2024-12-21 04:11:34 字数 1056 浏览 6 评论 0原文

给出基于 z3c.form 的以下小部件 https://github.com/collective/Products.UserAndGroupSelectionWidget/blob/z3cform-widget/src/Products/UserAndGroupSelectionWidget/z3cform/widget.py

我想在某些浏览器视图中访问其设置和相应字段。由于小部件预先不知道架构和字段,因此我对获取小部件和字段需要哪些信息感兴趣。目前我已经提供了字段名和上下文,这对于架构来说似乎足够了 https://github.com/collective/Products.UserAndGroupSelectionWidget/blob/z3cform-widget/src/Products/UserAndGroupSelectionWidget/browser.py#L60

编辑:为了简化问题,我想访问在某些z3c表单及其小部件中定义的字段。除了将请求和上下文传递给 init 然后访问该字段之外,我找不到其他方法。有多功能适配器吗?

这个想法是有一个 z3c.form 小部件,人们可以将其挂接到执行 ajax 调用的任何字段中。该 ajax 请求需要传递参数,响应将查找小部件的使用位置以及设置。问题是,如何查找 z3c.form 字段以及需要哪些信息

Given following widget based on z3c.form https://github.com/collective/Products.UserAndGroupSelectionWidget/blob/z3cform-widget/src/Products/UserAndGroupSelectionWidget/z3cform/widget.py

I would like in some browserview to access its settings and corresponding field. Since Widget does not know the schema and field upfront, I'm interested in what information do I need to get widget and field. Currently I have available the fieldname and context, which seemed to be enough for archtypes https://github.com/collective/Products.UserAndGroupSelectionWidget/blob/z3cform-widget/src/Products/UserAndGroupSelectionWidget/browser.py#L60

EDIT: To simplify the question, I would like to access a field that is defined in some z3c form and its widget. I could not find other way except passing request and context to form init and then accessing the field. Is there a multiadapter?

The idea is to have a z3c.form widget that people hook into whatever field which does an ajax call. That ajax request needs to pass parameters and response will lookup where widget was used and with what settings. The question is, how to lookup the z3c.form field and which information is needed to do so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里南柯 2024-12-28 04:11:34

获取字段

如果可以获得架构,就可以获得字段。

对于敏捷内容类型,如果您知道字段名称和类型的 Portal_type,则可以从类型的工厂类型信息 (FTI) 获取架构。

因此,如果我们知道portal_typefield_name

from zope.component import getUtility
from plone.dexterity.interfaces import IDexterityFTI
fti = getUtility(IDexterityFTI, name=portal_type)
schema = fti.lookupSchema()
field = schema.get(field_name)

获取小部件

来自z3c.form文档:http://packages.python.org/z3c.form/widget.html

该小部件是一个多适配器,因此如果您有该字段,您可以像这样得到它:

ageWidget = zope.component.getMultiAdapter((field, request),
    interfaces.IFieldWidget)

重要提示:如果您通过 plone.autoform 指定了一个小部件,则不会获取该小部件。 plone.autoformz3c.form.field.Field 对象上手动设置 widgetFactory(与 zope .schema 字段!)。那么,获取小部件的最佳方法就是您已经完成的操作,即手动调用启动 FieldWidget

例如,如果您想要 UserAndGroupSelectionWidget

widget = FieldWidget(field, UserAndGroupSelectionWidget(field, request))

PS 因为我也在集体中并使用选择器小部件,所以我已经为您更新了代码;)

Getting the Field

If you can get the schema, you can get the field.

For a dexterity content type, if you know the field name and the type's portal_type, you can get the schema from the type's Factory Type Information (FTI).

So, if we know portal_type and field_name:

from zope.component import getUtility
from plone.dexterity.interfaces import IDexterityFTI
fti = getUtility(IDexterityFTI, name=portal_type)
schema = fti.lookupSchema()
field = schema.get(field_name)

Getting the Widget

From the z3c.form documentation: http://packages.python.org/z3c.form/widget.html

The widget is a multiadapter, so if you have the field, you can get it like so:

ageWidget = zope.component.getMultiAdapter((field, request),
    interfaces.IFieldWidget)

Important: If you have however specified a widget via plone.autoform, then that widget won't be fetched. plone.autoform manually sets a widgetFactory on the z3c.form.field.Field object (which is not the same as the zope.schema Field!). The best way to get the widget then, is what you have already done, by manually calling initiating a FieldWidget.

So for example if you want the UserAndGroupSelectionWidget:

widget = FieldWidget(field, UserAndGroupSelectionWidget(field, request))

P.S Since I'm also in the collective and use the picker widget, I've updated the code for you ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文