I added the constraint as stated in the 'Model constraints' section of documentation but it does not seem to trigger anything, does it have to be included within the class definition? Is there something missing?
# -*- coding: utf-8 -*-
from openerp import models, fields, api
from openerp.exceptions import ValidationError
class sale_order_line(models.Model):
_inherit = "sale.order.line"
sale_margin_percent = fields.Float('Margen de Ventas (%)', (4, 2))
def update_sale_margin(self, cr, uid, ids, price_unit, purchase_price, discount):
print 'update_sale_margin'
....
return {'value': {'sale_margin_percent': sale_margin } }
def update_sale_price(self, cr, uid, ids, sale_margin_percent, purchase_price, discount):
print 'update_sale_price'
....
return {'value': {'price_unit': sale_price } }
@api.one
@api.constrains('sale_margin_percent')
def _check_margin(self):
print '_check_margin'
if self.sale_margin_percent >= 100:
raise ValidationError("El margen de venta no puede ser superior al 100%")
# all records passed the test, don't return anything
[****UPDATE*****]
Following zbik comment, I have rewritten the code as follows:
# -*- coding: utf-8 -*-
from openerp import models, fields, api, exceptions
class sale_order_line(models.Model):
_inherit = "sale.order.line"
sale_margin_percent = fields.Float('Margen de Ventas (%)', (4, 2))
@api.onchange('price_unit')
def update_sale_margin(self, price_unit, purchase_price, discount):
print 'update_sale_margin'
...
return {'value': {'sale_margin_percent': sale_margin } }
@api.onchange('sale_margin_percent', 'purchase_price')
def update_sale_price(self, sale_margin_percent, purchase_price, discount):
print 'update_sale_price'
...
return {'value': {'price_unit': sale_price } }
@api.constrains('sale_margin_percent')
def _check_margin(self):
print '_check_margin'
if self.sale_margin_percent >= 100:
raise exceptions.ValidationError("El margen de venta no puede ser superior al 100%")
# all records passed the test, don't return anything
The attribute added is:
<xpath expr="//field[@name='order_line']/tree//field[@name='price_unit']" position="attributes">
<attribute name="on_change">update_sale_margin(price_unit, purchase_price, discount)</attribute>
</xpath>
But I get the following error:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 537, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 574, in dispatch
result = self._call_function(**self.params)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 310, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/service/model.py", line 113, in wrapper
return f(dbname, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 307, in checked_call
return self.endpoint(*a, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 803, in __call__
return self.method(*args, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 403, in response_wrap
response = f(*args, **kw)
File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 944, in call_kw
return self._call_kw(model, method, args, kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 936, in _call_kw
return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper
return old_api(self, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 363, in old_api
result = method(recs, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/openerp/models.py", line 5864, in onchange
record._onchange_eval(name, field_onchange[name], result)
File "/usr/lib/python2.7/dist-packages/openerp/models.py", line 5782, in _onchange_eval
method_res = getattr(self._model, method)(*args)
TypeError: update_sale_margin() takes exactly 4 arguments (7 given)
Hope someone can help.