z3c.form的optgroup小部件。
z3c.formwidget.optgroup的Python项目详细描述
简介
这个包实现了一个小部件,它将select值分组到optgroups中。
用法
假设您有以下模式:
class IMySchema(Interface): country = schema.Choice( required=True, title=u"Country", vocabulary='countries', ) subdivision = schema.Choice( required=True, title=u"State", vocabulary='subdivisions', ) region = schema.Choice( required=True, title=u"County", vocabulary='regions', )
创建词汇表时(例如,使用SimpleVocabulary),而不是添加SimpleTerm项:
... for country in pycountry.countries: terms.append(SimpleTerm(value=country.alpha2, token=country.alpha2, title=country.name)) ...
您可以添加OptgroupTerm项:
from z3c.formwidget.optgroup.widget import OptgroupTerm ... country_list = countries(context) for item in pycountry.subdivisions: parent = country_list.getTermByToken(item.country_code).title terms.append(OptgroupTerm(value=item.code, token=item.code, title=item.name, optgroup=parent)) ...
在您的表单中,只需分配OptgroupFieldWidget:
from z3c.formwidget.optgroup.widget import OptgroupFieldWidget class MyForm(form.Form): fields = field.Fields(IMySchema) fields['subdivision'].widgetFactory = OptgroupFieldWidget fields['region'].widgetFactory = OptgroupFieldWidget
贡献者
Thomas Massmann,作者
更改日志
1.2(2012-05-01)
- 修复了显示模式模板的错误HTML标记。 [托马斯·马斯曼]
1.1(2012-04-26)
- 始终将无值消息显示为第一项。 [托马斯·马斯曼]
1.0.1(2012-04-14)
- manifest.in缺少一些条目。 [托马斯·马斯曼]
1.0(2012-04-14)
- 初次发布。 [托马斯·马斯曼]