Functional Fields的一个例子
-
假设我们创建了一个contract对象:
<br />class hr_contract(osv.osv):<br /> _name = 'hr.contract'<br /> _description = 'Contract'<br /> _columns = {<br /> 'name' : fields.char('Contract Name', size=30, required=True),<br /> 'employee_id' : fields.many2one('hr.employee', 'Employee', required=True),<br /> 'function' : fields.many2one('res.partner.function', 'Function'),<br /> }<br />hr_contract()<br />
如果添加一个字段要通过看它的current contract来检索员工,我们使用functional field。对象hr_employee这样继承:<br />class hr_employee(osv.osv):<br /> _name = "hr.employee"<br /> _description = "Employee"<br /> _inherit = "hr.employee"<br /> _columns = {<br /> 'contract_ids' : fields.one2many('hr.contract', 'employee_id', 'Contracts'),<br /> 'function' : fields.function(<br /> _get_cur_function_id,<br /> type='many2one',<br /> obj="res.partner.function",<br /> method=True,<br /> string='Contract Function'),<br /> }<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 /> for i in ids:<br /> #get the id of the current function of the employee of identifier "i"<br /> sql_req= """<br /> SELECT f.id AS func_id<br /> FROM hr_contract c<br /> LEFT JOIN res_partner_function f ON (f.id = c.function)<br /> WHERE<br /> (c.employee_id = %d)<br /> """ % (i,)<br /><br /> cr.execute(sql_req)<br /> sql_res = cr.dictfetchone()<br /><br /> if sql_res: #The employee has one associated contract<br /> res[i] = sql_res['func_id']<br /> else:<br /> #res[i] must be set to False and not to None because of XML:RPC<br /> # "cannot marshal None unless allow_none is enabled"<br /> res[i] = False<br /> 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有着完全不同的意思。 -
[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 /> _name = 'hr.contract'<br /> _description = 'Contract'<br /> _columns = {<br /> 'name' : fields.char('Contract Name', size=30, required=True),<br /> 'employee_id' : fields.many2one('hr.employee', 'Employee', required=True),<br /> 'function' : fields.many2one('res.partner.function', 'Function'),<br /> }<br />hr_contract()<br />
如果添加一个字段要通过看它的current contract来检索员工,我们使用functional field。对象hr_employee这样继承:<br />class hr_employee(osv.osv):<br /> _name = "hr.employee"<br /> _description = "Employee"<br /> _inherit = "hr.employee"<br /> _columns = {<br /> 'contract_ids' : fields.one2many('hr.contract', 'employee_id', 'Contracts'),<br /> 'function' : fields.function(<br /> _get_cur_function_id,<br /> type='many2one',<br /> obj="res.partner.function",<br /> method=True,<br /> string='Contract Function'),<br /> }<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 /> for i in ids:<br /> #get the id of the current function of the employee of identifier "i"<br /> sql_req= """<br /> SELECT f.id AS func_id<br /> FROM hr_contract c<br /> LEFT JOIN res_partner_function f ON (f.id = c.function)<br /> WHERE<br /> (c.employee_id = %d)<br /> """ % (i,)<br /><br /> cr.execute(sql_req)<br /> sql_res = cr.dictfetchone()<br /><br /> if sql_res: #The employee has one associated contract<br /> res[i] = sql_res['func_id']<br /> else:<br /> #res[i] must be set to False and not to None because of XML:RPC<br /> # "cannot marshal None unless allow_none is enabled"<br /> res[i] = False<br /> return res<br />
[size=12pt][color=red]这是官方的文档,有的地方我做了小小的翻译,有的地方我怕翻译错了给帮助我的人带来不便。我主要是三个note不是很理解原理也不清晰,希望版主或者哪位大哥今天心情舒爽,能够指点指点!谢谢![/color][/size]
[/quote]
请问怎么返回html类型??并且在视图内显示出来。。。返回的html是img