跳至内容
菜单
此问题已终结
1 回复
7388 查看

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
8100
3
8月 20
9788
1
2月 22
3633
1
1月 22
10479
0
4月 19
4288