Skip to Content
Menu
This question has been flagged
3 Replies
9369 Views

Good day,

i'm working with recruitment module, i added 2 fields fecha_nacimiento (birtdate) and edad (age), and when i write the birthdate automatically appear the age...that works perfectly....


but when i'm going to create a new applicant, this error appears:

Odoo Server Error

Traceback (most recent call last):

File "/home/gio666/odoo/openerp/http.py", line 537, in _handle_exception

return super(JsonRequest, self)._handle_exception(exception)

File "/home/gio666/odoo/openerp/http.py", line 574, in dispatch

result = self._call_function(**self.params)

File "/home/gio666/odoo/openerp/http.py", line 310, in _call_function

return checked_call(self.db, *args, **kwargs)

File "/home/gio666/odoo/openerp/service/model.py", line 113, in wrapper

return f(dbname, *args, **kwargs)

File "/home/gio666/odoo/openerp/http.py", line 307, in checked_call

return self.endpoint(*a, **kw)

File "/home/gio666/odoo/openerp/http.py", line 803, in __call__

return self.method(*args, **kw)

File "/home/gio666/odoo/openerp/http.py", line 403, in response_wrap

response = f(*args, **kw)

File "/home/gio666/odoo/openerp/addons/web/controllers/main.py", line 948, in call_kw

return self._call_kw(model, method, args, kwargs)

File "/home/gio666/odoo/openerp/addons/web/controllers/main.py", line 940, in _call_kw

return checked_call(request.db, *args, **kwargs)

File "/home/gio666/odoo/openerp/service/model.py", line 113, in wrapper

return f(dbname, *args, **kwargs)

File "/home/gio666/odoo/openerp/addons/web/controllers/main.py", line 939, in checked_call

return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)

File "/home/gio666/odoo/openerp/api.py", line 241, in wrapper

return old_api(self, *args, **kwargs)

File "/home/gio666/odoo/openerp/api.py", line 363, in old_api

result = method(recs, *args, **kwargs)

File "/home/gio666/odoo/openerp/models.py", line 5897, in onchange

record._onchange_eval(name, field_onchange[name], result)

File "/home/gio666/odoo/openerp/models.py", line 5815, in _onchange_eval

method_res = getattr(self._model, method)(*args)

File "/home/gio666/odoo/openerp/api.py", line 241, in wrapper

return old_api(self, *args, **kwargs)

File "/home/gio666/odoo/openerp/addons/hr_recruitment/hr_recruitment.py", line 364, in onchange_edad

edad = (datetime.now().date() - datetime.strptime(fecha_nacimiento, '%Y-%m-%d').date()).days / 365

TypeError: must be string, not bool


i think that problem is 'cause the field fecha_nacimiento(birthdate) is empty.. how can i solve it?, this is the code

def onchange_edad(self, cr, uid, ids, fecha_nacimiento, context=None):

res = {}

edad = (datetime.now().date() - datetime.strptime(fecha_nacimiento, '%Y-%m-%d').date()).days / 365

if edad < 0:

edad = 0

res = {'edad': edad}

return {'value': res}


and xml line:

<field name="fecha_nacimiento" on_change="onchange_edad(fecha_nacimiento)" placeholder="dd/mm/yyyy"/>

<field name="edad"/>


Avatar
Discard
Best Answer

When creating a new applicant the birthdate field is normally empty, but the on change function is triggered. In that case you can add a check if the  birthdate is not empty then do your calculation, else do nothing:

def onchange_edad(self, cr, uid, ids, fecha_nacimiento, context=None): 
    res = {}
    if fecha_nacimiento:
        edad = (datetime.now().date() - datetime.strptime(fecha_nacimiento, '%Y-%m-%d').d ate()).days / 365
        if edad < 0:
            edad = 0
        res = {'edad': edad}     
    return {'value': res}

 

Avatar
Discard
Author Best Answer

Thank you Tarek, it works.

Avatar
Discard
Related Posts Replies Views Activity
0
Oct 15
3071
0
Apr 15
2693
2
Aug 21
37218
1
Apr 16
3008
2
Sep 16
6547