Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
1 Răspunde
1628 Vizualizări

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.                          

                          

Imagine profil
Abandonează
Cel mai bun răspuns

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


Imagine profil
Abandonează
Autor

Thanks it works : )