约束 Odoo 10 上部字段
Upper field on constraint Odoo 10
我想在 Odoo 10 中的数据库中插入一条记录之前上一个字段。
我试过了,效果很好:
@api.one
@api.constrains('field')
def to_upper(self):
if self.field == "x":
self.field = self.field.upper()
但是如果我添加一个 else
@api.one
@api.constrains('field')
def to_upper(self):
if self.field == "x":
self.field = self.field.upper()
else:
raise ValidationError("Error")
总是 return else 的 ValidationError(当 if 语句为真时,因为没有 else 效果很好)
有人可以帮助我吗?谢谢
不得使用约束来操纵值。
Each time you try to set value of "field" it will trigger constrain
itself and this will leads to "RECURSION"!!
here you are always end up with raise ValidationError("Error") because of recursion.
==>here what happening is first when you are converting value of "field" from 'x' to 'X' and setting it again to field doing this calls constrain again with value 'X' and your condition becomes False and raises Error.
在 if & else 中输入打印语句并检查控制台你会得到更好的主意!
检查这是否对您有帮助
@api.constrains('name')
def _check_name(self):
if self.name:
final_name=''
for letters in self.name:
if re.match(r'[a-zA-Z0-9\- .]',letters):
final_name += letters
else:
not_accept_letters = letters
raise ValidationError(_('The character that you entered has not accepted %s')% not_accept_letters)
if self.name.islower():
self.name = final_name.upper()
我想在 Odoo 10 中的数据库中插入一条记录之前上一个字段。
我试过了,效果很好:
@api.one
@api.constrains('field')
def to_upper(self):
if self.field == "x":
self.field = self.field.upper()
但是如果我添加一个 else
@api.one
@api.constrains('field')
def to_upper(self):
if self.field == "x":
self.field = self.field.upper()
else:
raise ValidationError("Error")
总是 return else 的 ValidationError(当 if 语句为真时,因为没有 else 效果很好)
有人可以帮助我吗?谢谢
不得使用约束来操纵值。
Each time you try to set value of "field" it will trigger constrain itself and this will leads to "RECURSION"!!
here you are always end up with raise ValidationError("Error") because of recursion.
==>here what happening is first when you are converting value of "field" from 'x' to 'X' and setting it again to field doing this calls constrain again with value 'X' and your condition becomes False and raises Error.
在 if & else 中输入打印语句并检查控制台你会得到更好的主意!
检查这是否对您有帮助
@api.constrains('name')
def _check_name(self):
if self.name:
final_name=''
for letters in self.name:
if re.match(r'[a-zA-Z0-9\- .]',letters):
final_name += letters
else:
not_accept_letters = letters
raise ValidationError(_('The character that you entered has not accepted %s')% not_accept_letters)
if self.name.islower():
self.name = final_name.upper()