Skip to Content
Menu
This question has been flagged
1 Reply
5803 Views

hi all

i need to create unique python constraint (by convert this sql constraint) to be applied for all records
_sql_constraints = [
        ('unique_mobile', 'unique (mobile)', 'Mobile number already exists!')
    ]
Thanks

Avatar
Discard
Author Best Answer

i found a solution

# Mobile Field to be unique
@api.constrains('mobile')
def _check_mobile_unique(self):
mobile_counts = self.search_count([('mobile', '=', self.mobile), ('id', '!=', self.id)])
print(mobile_counts)
if mobile_counts > 0:
raise ValidationError("Mobile number already exists!")
Avatar
Discard
Related Posts Replies Views Activity
5
Oct 24
28476
1
Jul 23
4400
2
Jul 22
9128
2
Oct 21
1945
0
Aug 21
1179