Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Is it possible to enforce a float field in a form to accept values only between 0 and 99?

By
E.M.
on 7/26/15, 2:19 AM 501 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.

0
E.M.
On 7/26/15, 3:51 AM

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.


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

zbik
on 7/27/15, 2:26 AM

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.

E.M.
on 7/27/15, 5:50 PM
0

zbik

--zbik--
5941
| 6 4 7
Lodz, Poland
--zbik--


zbik
On 7/26/15, 2:43 AM

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 


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

E.M.
on 7/26/15, 3:49 AM

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

E.M.
on 7/26/15, 3:58 AM
0

Tarek Mohamed Ibrahim

--Tarek Mohamed Ibrahim--
896
| 5 3 7
Gîza, Egypt
--Tarek Mohamed Ibrahim--

I am an old VFP developer on ERP

I have moved to 2p since Nov-2014 and started developing with Python on Odoo.

https://www.linkedin.com/profile/public-profile-settings?trk=prof-edit-edit-public_profile

tibrahim@2p.com.sa

Tarek Mohamed Ibrahim
On 7/26/15, 2:45 AM

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 


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

E.M.
on 7/26/15, 3:33 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 7/26/15, 2:19 AM
Seen: 501 times
Last updated: 7/27/15, 5:49 PM