I have boolean field and i want to make sql constraint that only 1 record in model could have this field checked. how can i do this?
class ProductPricelist(models.Model):
_inherit = 'product.pricelist'
priority = fields.Boolean('Priority')
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
I have boolean field and i want to make sql constraint that only 1 record in model could have this field checked. how can i do this?
class ProductPricelist(models.Model):
_inherit = 'product.pricelist'
priority = fields.Boolean('Priority')
Hi,
Use api.constrains that will check your record before persisting it in the DB. Like this:
@api.constrains('priority')
def _check_boolean(self):
// to boost the performance, you search for only one record that is different from the current one which has the //boolean checked. If found, then raise.
checked_bool = self.search([('id', '!=', self.id),('priority', '=', True)], limit=1)
if self.priority and checked_bool:
// Means that more than one record has the field checked.
raise ValidationError(_("There's already one checked boolean in record '%s'") % checked_bool.name)
Thanks.
Use the following code
@api.constrains('priority')
def _check_priority(self):
if self.priority:
checked_bool = self.search([('id', '!=', self.id),('priority', '=', True)])
if checked_bool:
raise ValidationError(_("There's already one priority is checked. Reference : %s") % checked_bool[0].name)
أنشئ حساباً اليوم لتستمتع بالخصائص الحصرية، وتفاعل مع مجتمعنا الرائع!
تسجيلالمنشورات ذات الصلة | الردود | أدوات العرض | النشاط | |
---|---|---|---|---|
|
9
مارس 16
|
60430 | ||
|
1
مارس 15
|
4508 | ||
|
1
مارس 15
|
5222 | ||
|
2
مارس 15
|
4207 | ||
|
0
نوفمبر 24
|
5 |