跳至內容
選單
此問題已被標幟
1 回覆
7390 瀏覽次數

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.

頭像
捨棄
最佳答案

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"))
頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
1
8月 15
8101
3
8月 20
9790
1
2月 22
3633
1
1月 22
10479
0
4月 19
4288