Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
1 Beantwoorden
1640 Weergaven

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
Annuleer
Beste antwoord

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
Annuleer
Auteur

Thanks it works : )