跳至內容
選單
此問題已被標幟
1 回覆
1653 瀏覽次數

Hi,all:

    In project module,I use this code to avoid project name repeated:

@api.constrains('name')
def check_info(self):
info_id = self.sudo().search([('name', '=', self.name)])
if len(info_id) > 1:
raise Warning('%s The project was duplicate and could not be created.' % info_id)

   But now due to new demand,when both the project name and the Project Manager name are repeated,the project can't be created.I try to change my code to this but some error happens:

@api.constrains('name', 'user_id')
def check_info(self):
info_id = self.sudo().search([('name', '=', self.name), ('user_id', '=', self.user_id)])
if len(info_id) > 1:
raise Warning('%s The project was duplicate and could not be created.' % info_id)

    The error says "Invalid value res.users(2,) in domain term ('user_id', '=', res.users(2,))",I guess the reason is that 'user_id' is a "Many2one" field and can't compare like the above code.So how should I change my code?

    Thanks for your help.                          

                          

頭像
捨棄
最佳答案

You are passing recordset of the user instead of ID. It should be "self.user_id.id"

self.sudo().search([('name', '=', self.name), ('user_id', '=', self.user_id.id)])


頭像
捨棄
作者

Thanks it works : )