Skip to Content
Menu
This question has been flagged
1 Reply
2539 Views
registration_no = fields.Char(string="Registration No")

i want to set this filed unique

Avatar
Discard
Best Answer

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

Avatar
Discard