Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
10603 Lượt xem

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


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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}

 

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

Thank you Tarek, it works.

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 10 15
3792
0
thg 1 25
4267
2
thg 8 21
39178
1
thg 4 16
3979
2
thg 9 16
7411