registration_no = fields.Char(string="Registration No")
i want to set this filed unique
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
registration_no = fields.Char(string="Registration No")
i want to set this filed unique
Hi,
For this you can use sql_constrains or using constrains decorator in Odoo ORM. Sql constrains will be much faster.
Using SQL constrains you can do as follows:
_sql_constraints = [
('ref_unique', 'unique(registration_no)', 'Registration No. should be unique.'),
]
Using constrains decorator:
@api.constrains('registration_no')
def _check_registration_no(self):
for rec in self:
domain = [('registration_no', '=', rec.registration_no)]
count = self.sudo().search_count(domain)
if count > 1:
raise ValidationError(_("The Registration No should be unique"))
For More:
1. Sql Constrains: https://www.youtube.com/watch?v=1D_rBo8Vb44
2. Odoo Constrains : https://www.youtube.com/watch?v=50cecNF3OyQ
Thanks
Create an account today to enjoy exclusive features and engage with our awesome community!
Sign up