Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
1 Balas
7389 Tampilan

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
Buang
Jawaban Terbai

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
Buang
Post Terkait Replies Tampilan Aktivitas
1
Agu 15
8101
3
Agu 20
9789
1
Feb 22
3633
1
Jan 22
10479
0
Apr 19
4288