我在product.pricelist.item中添加了一个布尔字段
class product_pricelist_item(models.Model):
_inherit = 'product.pricelist.item'
myfield = fields.Boolean(string="CheckMark")现在,product.pricelist.item中有多行代码。
(验证)我希望用户不允许使True多个myfield,一个字段一次可以是True。
我尝试在product.pricelist.item中这样做,方法是给它一个计数器,并向计数器传递myfields的编号,即True。
但这给了我错误。
未定义全局名称“_get_counter”
def _get_counter(self):
for r in self:
p=[]
p= r.env['product.pricelist.item'].search_read([('myfield', '=', True)], ['myfield'])
counter = len(p)
return counter
@api.constrains('myfield')
def _check_myfield(self):
counter = _get_counter(self)
for r in self:
if counter > 1:
raise exceptions.ValidationError("Multiple myfield In a PriceList of a Product is not allowed.")第二个问题是:-
创建价格表项目并单击“价格表中的保存”时,它不会反映数据库中的数据。当您单击价格表保存时,它反映的是data...why吗?
发布于 2016-08-25 15:36:17
使用self,我们可以调用当前类的方法。
尝试使用以下代码:
替换码
counter = _get_counter(self)使用
counter = self._get_counter()发布于 2016-08-26 11:12:35
_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.")https://stackoverflow.com/questions/39146304
复制相似问题