Odoo中文社区可以通过以下三个域名访问:shine-it.net , odoocn.org,odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

Functional Fields的一个例子



  • 假设我们创建了一个contract对象:

    <br />class hr_contract(osv.osv):<br />&nbsp; &nbsp; _name = &#039;hr.contract&#039;<br />&nbsp; &nbsp; _description = &#039;Contract&#039;<br />&nbsp; &nbsp; _columns = {<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;name&#039; : fields.char(&#039;Contract Name&#039;, size=30, required=True),<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;employee_id&#039; : fields.many2one(&#039;hr.employee&#039;, &#039;Employee&#039;, required=True),<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;function&#039; : fields.many2one(&#039;res.partner.function&#039;, &#039;Function&#039;),<br />&nbsp; &nbsp; }<br />hr_contract()<br />
    


    如果添加一个字段要通过看它的current contract来检索员工,我们使用functional field。对象hr_employee这样继承:

    <br />class hr_employee(osv.osv):<br />&nbsp; &nbsp; _name = &quot;hr.employee&quot;<br />&nbsp; &nbsp; _description = &quot;Employee&quot;<br />&nbsp; &nbsp; _inherit = &quot;hr.employee&quot;<br />&nbsp; &nbsp; _columns = {<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;contract_ids&#039; : fields.one2many(&#039;hr.contract&#039;, &#039;employee_id&#039;, &#039;Contracts&#039;),<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;function&#039; : fields.function(<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _get_cur_function_id,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type=&#039;many2one&#039;,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj=&quot;res.partner.function&quot;,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method=True,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string=&#039;Contract Function&#039;),<br />&nbsp; &nbsp; }<br />hr_employee()<br />
    



    这里有三个[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]

    <br />def _get_cur_function_id(self, cr, uid, ids, field_name, arg, context):<br />&nbsp; &nbsp; for i in ids:<br />&nbsp; &nbsp; &nbsp; &nbsp; #get the id of the current function of the employee of identifier &quot;i&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; sql_req= &quot;&quot;&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; SELECT f.id AS func_id<br />&nbsp; &nbsp; &nbsp; &nbsp; FROM hr_contract c<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LEFT JOIN res_partner_function f ON (f.id = c.function)<br />&nbsp; &nbsp; &nbsp; &nbsp; WHERE<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (c.employee_id = %d)<br />&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot; % (i,)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; cr.execute(sql_req)<br />&nbsp; &nbsp; &nbsp; &nbsp; sql_res = cr.dictfetchone()<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; if sql_res: #The employee has one associated contract<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res[i] = sql_res&#91;&#039;func_id&#039;]<br />&nbsp; &nbsp; &nbsp; &nbsp; else:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #res[i] must be set to False and not to None because of XML:RPC<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # &quot;cannot marshal None unless allow_none is enabled&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res[i] = False<br />&nbsp; &nbsp; return res<br />
    



    [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对象:

    <br />class hr_contract(osv.osv):<br />&nbsp; &nbsp; _name = &#039;hr.contract&#039;<br />&nbsp; &nbsp; _description = &#039;Contract&#039;<br />&nbsp; &nbsp; _columns = {<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;name&#039; : fields.char(&#039;Contract Name&#039;, size=30, required=True),<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;employee_id&#039; : fields.many2one(&#039;hr.employee&#039;, &#039;Employee&#039;, required=True),<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;function&#039; : fields.many2one(&#039;res.partner.function&#039;, &#039;Function&#039;),<br />&nbsp; &nbsp; }<br />hr_contract()<br />
    


    如果添加一个字段要通过看它的current contract来检索员工,我们使用functional field。对象hr_employee这样继承:

    <br />class hr_employee(osv.osv):<br />&nbsp; &nbsp; _name = &quot;hr.employee&quot;<br />&nbsp; &nbsp; _description = &quot;Employee&quot;<br />&nbsp; &nbsp; _inherit = &quot;hr.employee&quot;<br />&nbsp; &nbsp; _columns = {<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;contract_ids&#039; : fields.one2many(&#039;hr.contract&#039;, &#039;employee_id&#039;, &#039;Contracts&#039;),<br />&nbsp; &nbsp; &nbsp; &nbsp; &#039;function&#039; : fields.function(<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _get_cur_function_id,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type=&#039;many2one&#039;,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj=&quot;res.partner.function&quot;,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method=True,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string=&#039;Contract Function&#039;),<br />&nbsp; &nbsp; }<br />hr_employee()<br />
    



    这里有三个[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]

    <br />def _get_cur_function_id(self, cr, uid, ids, field_name, arg, context):<br />&nbsp; &nbsp; for i in ids:<br />&nbsp; &nbsp; &nbsp; &nbsp; #get the id of the current function of the employee of identifier &quot;i&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; sql_req= &quot;&quot;&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; SELECT f.id AS func_id<br />&nbsp; &nbsp; &nbsp; &nbsp; FROM hr_contract c<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LEFT JOIN res_partner_function f ON (f.id = c.function)<br />&nbsp; &nbsp; &nbsp; &nbsp; WHERE<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (c.employee_id = %d)<br />&nbsp; &nbsp; &nbsp; &nbsp; &quot;&quot;&quot; % (i,)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; cr.execute(sql_req)<br />&nbsp; &nbsp; &nbsp; &nbsp; sql_res = cr.dictfetchone()<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; if sql_res: #The employee has one associated contract<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res[i] = sql_res&#91;&#039;func_id&#039;]<br />&nbsp; &nbsp; &nbsp; &nbsp; else:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #res[i] must be set to False and not to None because of XML:RPC<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # &quot;cannot marshal None unless allow_none is enabled&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res[i] = False<br />&nbsp; &nbsp; return res<br />
    



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

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