Skip to Content
Menu
This question has been flagged
1 Reply
1798 Views
Hi,all:

I'm trying to avoid project name repeated while creating a project by using this code:

@api.constrains('name')
def check_info(self):
info_id = self.search([('name', '=', self.name)])
if len(info_id) > 1:
raise Warning('%s project name repeated!' % info_id)
It will warn me when I try to create a project when there is another project
with same as I created.


However,if a project named "Project_A"and its privacy is "followers" and I'm not invited,I
can still create a project named "Project_A" without receiving a warning,in fact I wanna avoid
creating this "Project_A" again.
So how can I update my code or do some configuration to meet the requirements?Thanks for your
answer


Avatar
Discard

You can use method in the given answer as well as the sql constrains

Best Answer

Search duplicates under the SuperUser (sudo):

@api.constrains('name')
def check_info(self):
info_id = self.sudo().search([('name', '=', self.name)])
if len(info_id) > 1:
raise Warning('%s project name repeated!' % info_id)
Avatar
Discard