This question has been flagged
3 Replies
6268 Views

Hi currently doing a project, which is used for employee registration. here is my code..

error in my project,

TypeError: onchange_getage() takes at most 6 arguments (7 given)

here is my code

in jebatraining_view.xml

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

in jebatraining.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"

    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 {'value':{'age': 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'),
        }
jebatraining()

anyone let me to know this solution... Thanks......

Avatar
Discard
Best Answer

Hi,

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

For function field, you will not get dob parameter. For that you need to browse record. As you are defining onchange in xml file for dob, you will get age by return following: return {'value':{'age': current_age}}

You don't need to define function in age field.

Avatar
Discard
Author

THanks dude,,, it works,,, but error when, i was create entry and viewing entry. TypeError: onchange_getage() takes at most 6 arguments (7 given)

You dont having onchange function, you having function field remove in xml onchange parameter then check it.

Author

Sridhar, i want age calculation when i select dob in form to edit, suddenly calculate age show age in form, thats yi using on_change event in xml,,,

You using as a functional field when you save it value come automatically, here no need onchange remove it and then check.

Either remove onchange method or function field. If you want to show age while saving the record, make it as function field. Onchange will give you age on select of dob. it could be like: <field name="dob" on_change = "onchange_getage(dob)" string="Date of Birth"/> OR if you are using function field, you need to change the function like : onchange_getage(self, cr, uid, ids, field_name, arg, context=None):

Author

all dudes....!!! its working without errors.... thanks lot to all......

Best Answer

Change like this, _name is like employee.data, dont use employee_data, its make error. class jebatraining(osv.osv): _name = "employee.data" your code, and change xml

<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" 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> Try now.

Avatar
Discard
Best Answer

Hi;

Do you think that something like the following, can be used:

Extracted from

def _calculate_age(self, cr, uid, ids, field_name, arg, context=None):

    res = dict.fromkeys(ids, False)
    for ee in self.browse(cr, uid, ids, context=context):
        if ee.dob:
            dBday = datetime.strptime(ee.dob, OE_DFORMAT).date()
            dToday = datetime.now().date()
            res[ee.id] = (dToday - dBday).days / 365
    return res

 _columns = {
                           'age': fields.function(_calculate_age, type='integer', method=True, string='Age'),
}

Best regards.

Avatar
Discard