Python 如何在函数内部调用函数?Odoo9验证
我在product.pricelist.item中添加了一个布尔字段 现在product.pricelist.item中有多行 验证 我希望不允许用户将多个myfield设置为True,一次只能将一个字段设置为True 我尝试在product.pricelist.item中这样做,方法是给它一个计数器,并将myfields的数量传递给计数器,这些字段都是True 但这给了我错误 未定义全局名称“\u get\u counter” 第二个问题是:- 创建价目表项目并单击“保存在价目表中”时,它不会反映数据库中的数据。当您单击“价格表保存”时,它会反映数据……为什么会这样?使用self,我们可以调用当前类的方法 请尝试使用以下代码: 替换代码Python 如何在函数内部调用函数?Odoo9验证,python,validation,openerp,odoo-9,Python,Validation,Openerp,Odoo 9,我在product.pricelist.item中添加了一个布尔字段 现在product.pricelist.item中有多行 验证 我希望不允许用户将多个myfield设置为True,一次只能将一个字段设置为True 我尝试在product.pricelist.item中这样做,方法是给它一个计数器,并将myfields的数量传递给计数器,这些字段都是True 但这给了我错误 未定义全局名称“\u get\u counter” 第二个问题是:- 创建价目表项目并单击“保存在价目表中”时,它不会
counter = _get_counter(self)
与
循环输入_get_计数器不影响结果,搜索不依赖于记录,因此您可以使用:
def _get_counter(self):
pricelist_obj = self.env['product.pricelist.item']
counter = len(pricelist_obj.search_read([('myfield', '=', True)], ['myfield']))
return counter
@api.constrains('myfield')
def _check_myfield(self):
counter = self._get_counter()
if counter > 1:
raise exceptions.ValidationError("Multiple myfield In a PriceList of a Product is not allowed.")
错误:-\u get\u计数器正好接受1个参数2 givencounter=self。\u get\u计数器工作正常。
counter = _get_counter(self)
counter = self._get_counter()
def _get_counter(self):
pricelist_obj = self.env['product.pricelist.item']
counter = len(pricelist_obj.search_read([('myfield', '=', True)], ['myfield']))
return counter
@api.constrains('myfield')
def _check_myfield(self):
counter = self._get_counter()
if counter > 1:
raise exceptions.ValidationError("Multiple myfield In a PriceList of a Product is not allowed.")