Skip to Content
Menu
This question has been flagged
5 Replies
6235 Views

I am looking the user to enter a % (sales margin) between 0 and 99.

Is it possible to enforce that the float field will accept only values between x and y?

If so, how?,

and,

would it be possible to popup a message informing the user that he has to enter something between that x and y values?


Note: I am not looking to know how to add the field, just how to enforce that a defined field accept values only between x and y.

Avatar
Discard
Author Best Answer

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.


Avatar
Discard

You mix old and new api. on_change in new api works with decorator @api.onchange. Probably in this trouble.

Author

Thanks zbik, I have tried to unify to new api, but I can't make the on_change update functions work with the new api. You can see code above.

Best Answer

See Model constraints in documentation. Example from doc:

from openerp.exceptions import ValidationError

@api.constrains('age')
def _check_something(self):
    for record in self:
        if record.age > 20:
            raise ValidationError("Your record is too old: %s" % record.age)
    # all records passed the test, don't return anything 


Avatar
Discard
Author

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 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

Author

Sorry, see attached answer with proper formatting (-not enough karma to convert as an answer or delete de comment-).

Best Answer

I think you have to write an 'on_change' method and attache it in the xml to the field you need to enforce its value

field in xml will be like the following:

...
<field name="percent_field" on_change="onchange_percent_field(percent_field)"/>
...

you have to create a method onchange_percent_field and put in it your check, if the value is out of your range then you have to set the value of the field to its old value and  raise an exception 


Avatar
Discard
Author

I would prefer to let the on_change function for actual on_change activities. I see that more as a trigger than a constraint.

Related Posts Replies Views Activity
1
Nov 22
1785
2
Jul 24
480
1
Mar 22
1969
4
Jun 21
6092
1
Jan 21
2900