Skip to Content
Menú
This question has been flagged
1 Respondre
7415 Vistes

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
Descartar
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
Descartar
Related Posts Respostes Vistes Activitat
1
d’ag. 15
8132
3
d’ag. 20
9854
1
de febr. 22
3675
1
de gen. 22
10497
0
d’abr. 19
4324