Functional Fields的一个例子



  • 假设我们创建了一个contract对象:
    [code]
    class hr_contract(osv.osv):
        _name = 'hr.contract'
        _description = 'Contract'
        _columns = {
            'name' : fields.char('Contract Name', size=30, required=True),
            'employee_id' : fields.many2one('hr.employee', 'Employee', required=True),
            'function' : fields.many2one('res.partner.function', 'Function'),
        }
    hr_contract()
    [/code]
    如果添加一个字段要通过看它的current contract来检索员工,我们使用functional field。对象hr_employee这样继承:
    [code]
    class hr_employee(osv.osv):
        _name = "hr.employee"
        _description = "Employee"
        _inherit = "hr.employee"
        _columns = {
            'contract_ids' : fields.one2many('hr.contract', 'employee_id', 'Contracts'),
            'function' : fields.function(
                _get_cur_function_id,
                type='many2one',
                obj="res.partner.function",
                method=True,
                string='Contract Function'),
        }
    hr_employee()
    [/code]

    这里有三个[color=red]note[/color]:
    [list type=decimal]
    [li]type =’many2one’是因为function field 必须生成一个一个many2one field;function is declared as a many2one in hr_contract also.[/li]
    [li]obj =”res.partner.function” is used to specify that the object to use for the many2one field is res.partner.function.[/li]
    [li]We called our method _get_cur_function_id because its role is to return a dictionary whose keys are ids of employees, and whose corresponding values are ids of the function of those employees. The code of this method is:[/li]
    [/list]

    [code]
    def _get_cur_function_id(self, cr, uid, ids, field_name, arg, context):
        for i in ids:
            #get the id of the current function of the employee of identifier "i"
            sql_req= """
            SELECT f.id AS func_id
            FROM hr_contract c
              LEFT JOIN res_partner_function f ON (f.id = c.function)
            WHERE
              (c.employee_id = %d)
            """ % (i,)

            cr.execute(sql_req)
            sql_res = cr.dictfetchone()

            if sql_res: #The employee has one associated contract
                res[i] = sql_res['func_id']
            else:
                #res[i] must be set to False and not to None because of XML:RPC
                # "cannot marshal None unless allow_none is enabled"
                res[i] = False
        return res
    [/code]

    [size=12pt][color=red]这是官方的文档,有的地方我做了小小的翻译,有的地方我怕翻译错了给帮助我的人带来不便。我主要是三个note不是很理解原理也不清晰,希望版主或者哪位大哥今天心情舒爽,能够指点指点!谢谢![/color][/size]



  • 假设我们创建了一个contract对象:
    [code]
    class hr_contract(osv.osv):
        _name = 'hr.contract'
        _description = 'Contract'
        _columns = {
            'name' : fields.char('Contract Name', size=30, required=True),
            'employee_id' : fields.many2one('hr.employee', 'Employee', required=True),
            'function' : fields.many2one('res.partner.function', 'Function'),
        }
    hr_contract()
    [/code]
    如果添加一个字段要通过看它的current contract来检索员工,我们使用functional field。对象hr_employee这样继承:
    [code]
    class hr_employee(osv.osv):
        _name = "hr.employee"
        _description = "Employee"
        _inherit = "hr.employee"
        _columns = {
            'contract_ids' : fields.one2many('hr.contract', 'employee_id', 'Contracts'),
            'function' : fields.function(
                _get_cur_function_id,
                type='many2one',
                obj="res.partner.function",
                method=True,
                string='Contract Function'),
        }
    hr_employee()
    [/code]

    这里有三个[color=red]note[/color]:
    [list type=decimal]
    [li]type =’many2one’是因为function field 必须生成一个一个many2one field;function is declared as a many2one in hr_contract also.[/li]
    [li]obj =”res.partner.function” is used to specify that the object to use for the many2one field is res.partner.function.[/li]
    [li]We called our method _get_cur_function_id because its role is to return a dictionary whose keys are ids of employees, and whose corresponding values are ids of the function of those employees. The code of this method is:[/li]
    [/list]

    [code]
    def _get_cur_function_id(self, cr, uid, ids, field_name, arg, context):
        for i in ids:
            #get the id of the current function of the employee of identifier "i"
            sql_req= """
            SELECT f.id AS func_id
            FROM hr_contract c
              LEFT JOIN res_partner_function f ON (f.id = c.function)
            WHERE
              (c.employee_id = %d)
            """ % (i,)

            cr.execute(sql_req)
            sql_res = cr.dictfetchone()

            if sql_res: #The employee has one associated contract
                res[i] = sql_res['func_id']
            else:
                #res[i] must be set to False and not to None because of XML:RPC
                # "cannot marshal None unless allow_none is enabled"
                res[i] = False
        return res
    [/code]

    [size=12pt][color=red]这是官方的文档,有的地方我做了小小的翻译,有的地方我怕翻译错了给帮助我的人带来不便。我主要是三个note不是很理解原理也不清晰,希望版主或者哪位大哥今天心情舒爽,能够指点指点!谢谢![/color][/size]


  • 管理员

    过年却逢阴雨连绵,很是不爽 >:(

    其 实只要搞明白函数字段所指定的_get_cur_function_id方法所返回的值就很容易理解了。_get_cur_function_id方法返 回一个字典(dict), 其key为该函数字段所属的对象(hr.employee)的id,其值为对应Employee(员工)的contract(合同)中所指定的 Function(职务)的id. Function(职务)是一个独立的对象(res.partner.function),
    这在函数字段的obj属性中指明,该对象与Employee对象的关系则由type: many2one指定。

    其实这是一个函数字段最简单的用例,但是这个例子的选择非常的不好。估计写这个例子的人就是故意想把人给绕晕,员工的职务(Function)和函数字段(Function Field)这两个Function有着完全不同的意思。



  • 大兄弟,啥也不说了,太感谢了!
    我是被两个function给弄乱了~
    第一次得到这么详细的帮助!感动。
    啥也不说了,明天祝校长那里雨过天晴!



  • NICE~~~~~~~~~



  • [quote author=digitalsatori link=topic=5748.msg14198#msg14198 date=1360838298]
    ……

    其实这是一个函数字段最简单的用例,但是这个例子的选择非常的不好。估计写这个例子的人就是故意想把人给绕晕,员工的职务(Function)和函数字段(Function Field)这两个Function有着完全不同的意思。
    [/quote]

    确实非常不好!职务(Function)和函数(Funciton)只是单词相同,通过上下语境还是悟得出来!
    不好的地方在 对实体对象的业务关系关系根本没介绍,搞不清 Contract和Employee的关系,搞不清 Function(职务)和Employee的关系。
    看到这个官方例子时,我是纯凭空生扯 Contract、Funciton(职务)、Employee 的内容和关系,扯成一团麻,严重影响我的进度,导致对自己的学习能力产生怀疑,导致代码恐惧症加重~ 血、泪、汗~~~~
    现在我快扯顺了,我才不告诉你们我是怎么扯出来的呢~ 扭腰扭腰~ 坐等新人被这团乱麻困住,方能一解偶的怨念~ ^_^

    其实官方完全可以弄个简单的例子:比如:功能字段=字段1*字段2,那就很容易理解 so easy~
    官方弄个这么复杂的例子,故意嫌疑成分太大了,相当于打了点马赛克,只是薄码而已,各种恨~



  • [quote author=Peter Seng link=topic=5748.msg14197#msg14197 date=1360812735]
    假设我们创建了一个contract对象:
    [code]
    class hr_contract(osv.osv):
        _name = 'hr.contract'
        _description = 'Contract'
        _columns = {
            'name' : fields.char('Contract Name', size=30, required=True),
            'employee_id' : fields.many2one('hr.employee', 'Employee', required=True),
            'function' : fields.many2one('res.partner.function', 'Function'),
        }
    hr_contract()
    [/code]
    如果添加一个字段要通过看它的current contract来检索员工,我们使用functional field。对象hr_employee这样继承:
    [code]
    class hr_employee(osv.osv):
        _name = "hr.employee"
        _description = "Employee"
        _inherit = "hr.employee"
        _columns = {
            'contract_ids' : fields.one2many('hr.contract', 'employee_id', 'Contracts'),
            'function' : fields.function(
                _get_cur_function_id,
                type='many2one',
                obj="res.partner.function",
                method=True,
                string='Contract Function'),
        }
    hr_employee()
    [/code]

    这里有三个[color=red]note[/color]:
    [list type=decimal]
    [li]type =’many2one’是因为function field 必须生成一个一个many2one field;function is declared as a many2one in hr_contract also.[/li]
    [li]obj =”res.partner.function” is used to specify that the object to use for the many2one field is res.partner.function.[/li]
    [li]We called our method _get_cur_function_id because its role is to return a dictionary whose keys are ids of employees, and whose corresponding values are ids of the function of those employees. The code of this method is:[/li]
    [/list]

    [code]
    def _get_cur_function_id(self, cr, uid, ids, field_name, arg, context):
        for i in ids:
            #get the id of the current function of the employee of identifier "i"
            sql_req= """
            SELECT f.id AS func_id
            FROM hr_contract c
              LEFT JOIN res_partner_function f ON (f.id = c.function)
            WHERE
              (c.employee_id = %d)
            """ % (i,)

            cr.execute(sql_req)
            sql_res = cr.dictfetchone()

            if sql_res: #The employee has one associated contract
                res[i] = sql_res['func_id']
            else:
                #res[i] must be set to False and not to None because of XML:RPC
                # "cannot marshal None unless allow_none is enabled"
                res[i] = False
        return res
    [/code]

    [size=12pt][color=red]这是官方的文档,有的地方我做了小小的翻译,有的地方我怕翻译错了给帮助我的人带来不便。我主要是三个note不是很理解原理也不清晰,希望版主或者哪位大哥今天心情舒爽,能够指点指点!谢谢![/color][/size]
    [/quote]

    请问怎么返回html类型??并且在视图内显示出来。。。返回的html是img


登录后回复
 

与 Odoo 中文社区 的连接断开,我们正在尝试重连,请耐心等待