This question has been flagged
1 Reply
5916 Views

I'm trying to check if a many2many field has 2 specific records in it and then a constraint to it. I first tried to check if it had one specific record in it:

from openerp import models, fields, api, exceptions
class Partnerlead(models.Model):
    _inherit = 'crm.lead'
    @api.constrains('categ_ids')
    def _check_tags(self):
        for record in self:
            if (12) in record.categ_ids:
                raise exceptions.ValidationError('Error')

And I get an error:

ValidateError
Error while validating constraint
ValueError
Mixing apples and oranges: 12 in crm.case.categ(3, 8)

How do you exactly check if my crm.lead record has crm.case.categ field id 12 in its categ_ids m2m field? If I know how to check for a record, I can check for many. Thanks for advance.

Avatar
Discard
Best Answer

Hi Anti,

You can do it using the following code,

class PartnerLead(models.Model):
_inherit = 'crm.lead'

@api.constrains('categ_ids')
def _check_tags(self):
for record in self:
for rec in record.categ_ids:
if rec.id == 12:
raise ValidationError(_("Error"))
Avatar
Discard