首页 > 解决方案 > OpenERP 7:fields.function return [object Object]

问题描述

我想在 hr.employee 命名中添加 1 个字段contract_type。每次用户单击 中的员工姓名时,都会计算并存储此字段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_nameandres时,两者都显示正确的值:

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

它显示空字段(该函数也没有运行)。我该如何解决这个问题?非常感谢任何帮助。

标签: pythonodooopenerp-7

解决方案


解决了。我用这个改变res定义:

res = dict.fromkeys(ids, '')

推荐阅读