Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
1643 Widoki

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.                          

                          

Awatar
Odrzuć
Najlepsza odpowiedź

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)])


Awatar
Odrzuć
Autor

Thanks it works : )