تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
1 الرد
6624 أدوات العرض

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

الصورة الرمزية
إهمال
الكاتب أفضل إجابة

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!")
الصورة الرمزية
إهمال
المنشورات ذات الصلة الردود أدوات العرض النشاط
5
أكتوبر 24
32648
1
يوليو 23
7301
2
يوليو 22
11490
2
أكتوبر 21
3289
0
أغسطس 21
2025