【发布时间】:2017-08-28 06:11:37
【问题描述】:
我的 py 和 xml 文件代码如下所示 我想为文本字段设置 300 个字符的限制,那么此代码需要进行哪些更改?
.py 代码:
description': fields.text('Description', required=True)
.xml 代码:
<field name="description"/>
【问题讨论】:
标签: python-2.7 openerp odoo-8
我的 py 和 xml 文件代码如下所示 我想为文本字段设置 300 个字符的限制,那么此代码需要进行哪些更改?
.py 代码:
description': fields.text('Description', required=True)
.xml 代码:
<field name="description"/>
【问题讨论】:
标签: python-2.7 openerp odoo-8
您可能需要在字段级别设置 size 属性
description': fields.text('Description', required=True,size=150)
Odoo 会根据您在字段级别配置的大小属性自动调整大小。
听说不需要从 XML 部分设置任何类型的大小。
字段属性:size=150
这意味着用户将无法在该描述字段中添加超过 150 个字符
希望我的回答对你有所帮助。
【讨论】:
Text 字段类型不支持 size 属性。在 12.0 版本上测试(也许它在旧版本上工作?)。相反,您可以使用Char 字段类型,然后在xml 中的字段上添加widget="text",以获得文本字段接口,但保留Char 行为。
我在这里解释如何有效地使用约束。
@api.constrains('your_field')
@api.one
def _check_your_field(self):
if len(self.your_field) > 300:
raise ValidationError('Number of characters must not exceed 300')
别忘了导入
from odoo.exceptions import UserError, ValidationError
【讨论】: