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

AttributeError: 'jebatraining' object has no attribute 'onchange_getage'

By
Jothimani
on 3/28/14, 5:34 AM 1,425 views

Hi currently doing a project, which is used for employee registration. in my project i want calculate employee age automatically and it store into db. here is my code..

error in my project when i select in date of birth by using onchange event

Client Traceback (most recent call last):
  File "/usr/lib/pymodules/python2.7/openerp/addons/web/http.py", line 195, in dispatch
    response["result"] = method(self, **self.params)
  File "/usr/lib/pymodules/python2.7/openerp/addons/web/controllers/main.py", line 1078, in call_kw
    return self._call_kw(req, model, method, args, kwargs)
  File "/usr/lib/pymodules/python2.7/openerp/addons/web/controllers/main.py", line 1070, in _call_kw
    return getattr(req.session.model(model), method)(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/openerp/addons/web/session.py", line 40, in proxy
    result = self.proxy.execute_kw(self.session._db, self.session._uid, self.session._password, self.model, method, args, kw)
  File "/usr/lib/pymodules/python2.7/openerp/addons/web/session.py", line 28, in proxy_method
    result = self.session.send(self.service_name, method, *args)
  File "/usr/lib/pymodules/python2.7/openerp/addons/web/session.py", line 101, in send
    raise xmlrpclib.Fault(openerp.tools.exception_to_unicode(e), formatted_info)


Server Traceback (most recent call last):
  File "/usr/lib/pymodules/python2.7/openerp/addons/web/session.py", line 87, in send
    return openerp.netsvc.dispatch_rpc(service_name, method, args)
  File "/usr/lib/pymodules/python2.7/openerp/netsvc.py", line 289, in dispatch_rpc
    result = ExportService.getService(service_name).dispatch(method, params)
  File "/usr/lib/pymodules/python2.7/openerp/service/web_services.py", line 614, in dispatch
    res = fn(db, uid, *params)
  File "/usr/lib/pymodules/python2.7/openerp/osv/osv.py", line 169, in execute_kw
    return self.execute(db, uid, obj, method, *args, **kw or {})
  File "/usr/lib/pymodules/python2.7/openerp/osv/osv.py", line 123, in wrapper
    return f(self, dbname, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/openerp/osv/osv.py", line 179, in execute
    res = self.execute_cr(cr, uid, obj, method, *args, **kw)
  File "/usr/lib/pymodules/python2.7/openerp/osv/osv.py", line 166, in execute_cr
    return getattr(object, method)(cr, uid, *args, **kw)
AttributeError: 'jebatraining' object has no attribute 'onchange_getage'

in xml

<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
    <record id="view_employee_form" model="ir.ui.view">
    <field name="name">jebatraining.form_view</field>
    <field name="model">employee_data</field>
    <field name="type">form</field>
    <field name="arch" type="xml">
        <form string="Employee Details">
            <field name="ename" string="Employee Name"/>
            <field name="fname" string="Fathers Name"/>
            <field name="gender" string="Gender"/>
            <field name="dob" on_change = "onchange_getage(dob)" string="Date of Birth"/> 
            <field name="age" string="Age"/>
            <field name="mobile" string="Mobile"/>
            <field name="website" string="Website"/>            
            <field name="email" string="Email"/>
            <field name="address" string="Address"/>
            <field name="pincode" string="Pincode"/>
            <field name="doj" string="Date of Joining"/>
        </form>
    </field>
     </record>


    <record id="view_employee_tree" model="ir.ui.view">
    <field name="name">jebatraining.tree_view</field>
    <field name="model">employee_data</field>
    <field name="type">tree</field>
    <field name="arch" type="xml">
        <tree string="Employee Details">
            <field name="ename" string="Employee Name"/>
            <field name="fname" string="Fathers Name"/>
            <field name="gender" string="Gender"/>
            <field name="dob" on_change = "onchange_getage(dob)" string="Date of Birth"/>  
            <field name="age" string="Age"/>
            <field name="mobile" string="Mobile"/>
            <field name="salary" string="Salary"/>          
            <field name="email" string="Email"/>
            <field name="website" string="Website"/>
            <field name="address" string="Address"/>
            <field name="pincode" string="Pincode"/>
            <field name="doj" string="Date of Joining"/>
        </tree>
    </field>
     </record>

    <record id="action_employee" model="ir.actions.act_window">
        <field name="name">jebatrainig</field>
        <field name="res_model">employee_data</field>
        <field name="view_type">form</field>
        <field name="view_mode">list,form,kanban</field>
    </record>

    <menuitem 
    id="parent_menu"
    name="Jeba Training"
    sequence="1" />

    <menuitem
    id="sub_menu"
    name="Employee"
    parent="parent_menu"/>

        <menuitem
    action="action_employee"
    id="sub2_menu"
    name="Employee Details"
    parent="sub_menu"/>

</data>
</openerp>

in py

from datetime import date,datetime
from time import strptime
from dateutil import parser
from openerp.osv import osv, fields

class jebatraining(osv.osv):
    _name = "employee_data"
    _description = "Employee management"
    _columns = {
        'ename':fields.char('Employee Nmae', size=50, required=True),
        'fname':fields.char('Fathers Name', size=50, required=True),
        'gender':fields.selection((('male','Male'),('female','Female')),'Gender'),
        'dob':fields.date('Date of Birth', required=True),

        'mobile':fields.char('Mobile Number', size=10),
        'salary':fields.float('Salary'),        
        'email':fields.char('Email', size=30),
        'website':fields.char('Website', size=30),      
        'address':fields.text('Address'),
        'pincode':fields.integer('Pincode', size=6),
        'doj':fields.date('Date of joining'),
        }


    def onchange_getage(self,cr,uid,ids,dob,context=None):
        current_date=datetime.now()
        current_year=current_date.year
        birth_date=parser.parse(dob)
        current_age=current_year-birth_date.year
        val={
            'age':current_age
        }
        return{'value':val}

jebatraining()

Anyone help me. thanks

0

Med Said BARA

--Med Said BARA--
2664
| 5 5 7
Algeria
--Med Said BARA--
Med Said BARA
On 3/28/14, 7:59 AM

Hi;

In your py file, you should move the fuction definition onchange_getage before the fields definitions.

Something like this:

 class jebatraining(osv.osv):

_name = "employee_data"

_description = "Employee management"

   def onchange_getage(self,cr,uid,ids,dob,context=None):

    current_date=datetime.now()

    current_year=current_date.year

    birth_date=parser.parse(dob)

    current_age=current_year-birth_date.year


    return current_age

 _columns = {

    'ename':fields.char('Employee Nmae', size=50, required=True),

    'fname':fields.char('Fathers Name', size=50, required=True),

    'gender':fields.selection((('male','Male'),('female','Female')),'Gender'),

    'dob':fields.date('Date of Birth', required=True),


    'mobile':fields.char('Mobile Number', size=10),

    'salary':fields.float('Salary'),  

    'email':fields.char('Email', size=30),

    'website':fields.char('Website', size=30),  

    'address':fields.text('Address'),

    'pincode':fields.integer('Pincode', size=6),

    'doj':fields.date('Date of joining'),

    'age': fields.function(onchange_getage, type='integer', method=True, string='Age'),

    }

This is just a first try ... not sure that it works as it is

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

0 follower(s)

Stats

Asked: 3/28/14, 5:34 AM
Seen: 1425 times
Last updated: 3/16/15, 8:10 AM