Odoo Help


how to validate float field

on 7/16/13, 1:24 PM 5,566 views

Please I have two fields with float datatype, how do I validate these two fields to prompt a user to enter a value different from default value 0.00?



Grover Menacho

--Grover Menacho--
WebVision Enterprise Systems
| 5 7 8
La Paz, Bolivia
--Grover Menacho--

Odoo Developer

Grover Menacho
On 7/16/13, 1:46 PM

You have to write a create function on that object. There are loads of examples on the web. You just have to write a create function then check if those values are different from zero. If they are you call the create of the super. If they are zero you can create a osv error.

Please can you give me an example?

on 7/17/13, 7:08 AM

def create(self, cr, uid, vals, context=None):

    raise osv.except_osv(_('Warning'), _('Value must be different from zero'))
    return super(module_name, self).create(cr, uid, vals, context)
GH Mediacloud, Grover Menacho
on 7/17/13, 11:29 AM

It's a generic way to do it. But basically it's what you need.

GH Mediacloud, Grover Menacho
on 7/17/13, 11:30 AM

Thank you very much!

on 7/17/13, 12:04 PM

If it helped you please mark the answer as correct to help others.

GH Mediacloud, Grover Menacho
on 7/17/13, 12:11 PM

Please where do I mark it as correct answer?

on 7/17/13, 12:20 PM

Below your question there is a section that now says "1 answer". There is a tick circle below a number and that number is to vote for the answer. You have to do it on all your questions answered because this helps to many people to find quickly answers. If the answer is correct you have to check it, if other answer helped you, you have to vote for it.

GH Mediacloud, Grover Menacho
on 7/17/13, 12:56 PM

Hi Grover Menacho, I also want to know how to validate float data type time field. For example in the validation code float(time) it accepts the format only xx:yy and not accept other format time for example 100:00 pl given me sample code based on this. Thanks

on 1/13/14, 4:15 AM

I don't want to do it in create function. That validates when we save the entire form and that is a custom validation done by us. We need a way to have automatic validation just like the validation for the mandatory fields (Char, date) when they are empty. But integer and float have default value of 0. Because of that, the validation of mandatory field is not applied. Its not fair

on 11/15/14, 4:50 AM
Ajeng Shilvie
On 11/5/15, 12:50 AM

If you are using class models.Model not osv.osv, try this :

def change_value(self):
if self.your_float_field1 == 0 or self.your_float_field2 == 0 :
return {'warning':{'title':'Error!','message':'your message'}}


Akhil P Sivan

--Akhil P Sivan--

| 5 4 7
Kochi, India
--Akhil P Sivan--

Interested in exploring Odoo both technically & functionality wise.

email-id : akhilpsivan01@gmail.com

Akhil P Sivan
On 11/5/15, 2:10 AM


if you are working with new api, you may try this:

@api.constrains('field1', 'field2')
def _check_values(self):
if self.field1 == 0.0 or self.field2 == 0.0:
raise Warning(_('Values should not be zero.'))

About This Community

This platform 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.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)


Asked: 7/16/13, 1:24 PM
Seen: 5566 times
Last updated: 8/21/17, 12:14 AM