Odoo Help


Custom module Onchange function is not working in version 7 and not shows warning message

on 12/22/14, 2:26 AM 1,041 views

class hr_analytic_timesheet(osv.osv):
    _inherit = 'hr.analytic.timesheet'
    _columns = {
              'worked_hours':fields.float('Worked Hours'),
              'overtime_hours':fields.float('Over Time'),


  def onchange_worked_hours(self, cr, uid, ids, worked_hours, context=None):
        for i in self.read(cr,uid,ids,['worked_hours'],context=context):
             if i['worked_hours'] >8.00:
                 print("##########################inside worked hours")
                 raise osv.except_osv(_('Warning'),_('Worked Hours Cannot be greater than 8'))
             return True



 <record id="timesheet_tree_inherited_view" model="ir.ui.view">
        <field name="name">timesheet.tree.inherited</field>
        <field name="model">hr.analytic.timesheet</field>
        <field name="inherit_id" ref="hr_timesheet.hr_timesheet_line_tree"/>
        <field name="arch" type="xml">
         <xpath expr="//tree/field[@name='unit_amount']" position="before">
            <field name="worked_hours" on_change="onchange_worked_hours(worked_hours)"/>        
              <field name="total"/>




| 5 3 6
Jakarta, Indonesia
On 12/22/14, 5:17 AM

@LIBU, are you trying to validate the new value of worked_hours or the saved value?  Currently you are checking against the saved ['worked_hours'], if the record has been saved before (in the case of you editing the record).  As @Ludo has mentioned, the record have not been saved if you expect this to be triggered when you create a new record.

Also, wouldn't it be better if you use the 'warning' return value key instead of raising exception?

Yes @Ivan I want to validate the value when I create a new record.....It is not a saved data

on 12/22/14, 6:18 AM

So you should be using worked_hours variable directly as @Ludo had explained. if worked_hours >8.00 .... Also, please consider using 'warning' return as opposed to raising errors.

on 12/22/14, 6:56 AM

Sorry, forgot to mention, remove the for loop.

on 12/22/14, 6:56 AM

Ludo - 21South

--Ludo - 21South--
| 2 2 5
Zwijndrecht, Netherlands
--Ludo - 21South--


Ludo - 21South
On 12/22/14, 4:01 AM

Are you sure ids is not empty? Since you are on the on_change method, the object you are working on does not have to be saved yet.

Also, if you supply the worked hours (which can be a list of ids of the working_hours object), then you don't need a read on self, but rather on the object the worked_hours field is referencing.

Print/debug list the values in the beginnen of your method to be sure everything is supplied in the first place and see which types they are.

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

1 follower(s)


Asked: 12/22/14, 2:26 AM
Seen: 1041 times
Last updated: 3/16/15, 8:10 AM