Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
1 Vastaa
1641 Näkymät

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.                          

                          

Avatar
Hylkää
Paras vastaus

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


Avatar
Hylkää
Tekijä

Thanks it works : )