Search code examples
pythonormodoounique

Unique values but how to prevent with spelling mistakes - Odoo


I can use _sql_constraints as mentioned below to prevent duplicates but when user enter like:

Bakery

bakery

bakry

etc...

how i can prevent user to NOT enter this type of value?

> _sql_constraints = [
> 
>     ('categoryname_unique',
> 
>      'unique(name)',
> 
>      'each category should be unique'),
> 
> ]

Solution

  • at least i can avoid duplication with same spelling but in either case (case insensitive) using below code (thanks to odoo forum - @Neha Sharma-Kanak)

    @api.constrains('name')
    def _check_unique_brand(self):
        brand_ids = self.search([]) - self
    
        value = [x.name.lower() for x in brand_ids]
    
        if self.name and self.name.lower() in value:
            raise ValidationError(_('The Brand Name is already Exist'))
    
        return True