如何在文本字段中设置字符限制..?

How to set character limit in Text field ..?

我的 py 和 xml 文件代码如下所示 我想为文本字段设置 300 个字符限制,那么此代码需要进行哪些更改?

.py 代码:

description': fields.text('Description', required=True)

.Xml 代码:

<field name="description"/>

您可能必须在字段级别设置大小属性

description': fields.text('Description', required=True,size=150)

Odoo 会根据您在字段级别配置的尺寸属性自动调整尺寸。

听说 XML 部分不需要设置任何类型的尺寸。

字段属性:size=150

这意味着用户将无法在该描述字段中添加超过 150 个字符

希望我的回答对您有所帮助

在这里我将解释如何有效地使用约束。

@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