I want to add 1 field in hr.employee named contract_type
. this field will be compute and store each time a user click on an employee's name in Human Resources > Employees
. I created xml in a new module with this code:
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="hr_employee_view_form_inherit" model="ir.ui.view">
<field name="name">hr.employee.view.form.inherit</field>
<field name="model">hr.employee</field>
<field name="inherit_id" ref="hr_contract.hr_hr_employee_view_form2"/>
<field name="arch" type="xml">
<xpath expr="//group[@string='Contract']/field[@name='medic_exam']" position="before">
<field name="contract_type" string="Contract Type"/>
</xpath>
</field>
</record>
</data>
In py, contract_type
is defined as fields.function
.
from openerp import addons
import logging
from openerp.osv import fields, osv
from openerp.tools.translate import _
from openerp import tools
_logger = logging.getLogger(__name__)
class inherit_hr_employee(osv.osv):
_inherit = 'hr.employee'
def _get_contract_type(self, cr, uid, ids, field_name, arg, context=None):
res = {}
contract_name = ""
for i in ids:
sql_req= """
SELECT *
FROM hr_contract
WHERE employee_id = %d
""" % (i,)
cr.execute(sql_req)
sql_res = cr.dictfetchone()
flag = False
_is_empty = sql_res.get("date_end")
contract_type = sql_res.get("type_id")
if not _is_empty:
flag = True
if flag:
sql_contract_type = """
SELECT *
FROM hr_contract_type
WHERE id = %d
""" % (contract_type,)
cr.execute(sql_contract_type)
sql_contract = cr.dictfetchone()
contract_name = sql_contract.get("name")
for employee in self.browse(cr, uid, ids, context=context):
res[employee.id] = {
'contract_type': str(contract_name)
}
return res
_columns = {
'contract_type' : fields.function(_get_contract_type, type='text', string='Contract Type', method=True, readonly=True, size=20)
}
inherit_hr_employee()
When I print contract_name
and res
, both showing the correct value:
PERMANENT
{366: {'contract_type': 'PERMANENT'}}
But in view, field contract_type
showing text [object Object]
. there'e no error in terminal and inspect html page (Ctrl+Shift+I). I didn't put store=True
in my code above because when I defined store
, the function didn't even run.
I've tried to add another field:
_columns = {
'type' : fields.function(_get_contract_type, type='char', string='Contract Type', size=20,
store = {
'hr.employee': (lambda self,cr,uid,ids,c=None: ids, ['contract_type'], 10)
}),
'contract_type' : fields.text(string='Contract Type', readonly=True, size=30)
}
It shows empty field (the function didn't run as well). How can I solve this? Any help is highly appreciated.
Solved. I change res
definition with this:
res = dict.fromkeys(ids, '')