David Hamilton
David Hamilton

Reputation: 339

Odoo 9 customize module with field validation

I am using a new module to customize and existing app. The module installs, and the field appears correctly and saves correctly. The issue is that my custom constraints are ignored.

Here is the full code from my models.py file:

# -*- coding: utf-8 -*-

from openerp import models, fields, api
import logging
_logger = logging.getLogger(__name__)
# class myfieldsinsaleorder(models.Model):
#     _name = 'myfieldsinsaleorder.myfieldsinsaleorder'

class partnercustomfields(models.Model):
    _inherit = "res.partner"

    def test(self):
        return False

    x_vend_account_ref = fields.Char(string="Our Account ID",
                                  help='Our account number with this vendor.',
                                  size=20)
    _constraints = [(test,"Invalid Data",[x_vend_account_ref])]

Upvotes: 1

Views: 1431

Answers (2)

David Hamilton
David Hamilton

Reputation: 339

Thanks to mokiSRB for putting me on the right track. His suggestion of using @api.constrains is correct but my purposed return value was also wrong.

In looking at other uses of @api.constrains in other modules I find raise UserError. This works, but the method is deprecated. As best as I can gather the correct method to error to raise is ValidationError as this produces the expected result.

@api.constrains('x_vend_account_ref')
def customvalidation(self):
    raise ValidationError('The Field Is Not valid')

Upvotes: 1

mokiSRB
mokiSRB

Reputation: 1162

Constraints should be used like this:

@api.constrains("x_vend_account_ref") def test(self): return False

Upvotes: 0

Related Questions