我想在名为contract_type
的hr.employee中添加1个字段。每当用户在Human Resources > Employees
中单击雇员的姓名时,此字段将进行计算和存储。我使用以下代码在新模块中创建了xml:
<?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>
在py中,contract_type
被定义为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()
当我打印contract_name
和res
时,都显示正确的值:
PERMANENT
{366: {'contract_type': 'PERMANENT'}}
但是在视图中,字段contract_type
显示了文本[object Object]
。在终端中没有错误,并检查html页面(Ctrl + Shift + I)。我没有在上面的代码中放入store=True
,因为当我定义store
时,该功能甚至没有运行。
我尝试添加另一个字段:
_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)
}
它显示为空字段(该函数也未运行)。我该如何解决?我们非常感谢您的帮助。
答案 0 :(得分:0)
已解决。我用以下方法更改res
的定义:
res = dict.fromkeys(ids, '')