如何在openERP 7中添加函数?
我在尝试为openERP 7版本创建一个新模块。在我的类里有这段代码:
_columns = {
'hour_from' : fields.float('Work from', required=True),
'hour_to' : fields.float("Work to", required=True),
'totalhour': fields.function(_total, method=True, string='Total Attendance', multi="_total"),
}
我找不到任何方法在我的类中添加一个函数。我需要的这个函数是返回hour_from
和hour_to
的总和。有没有人能帮帮我?
我在声明我的_columns之前尝试了这段代码:
def _total(self, cr, uid, ids, name, args, context=None):
res = {}
res['totalhour'] = hour_from + hour_to
return res
当我重启服务器时,我收到了这个错误:
No handler found.
(来自其他帖子更新)
好吧,这是我的代码:
def _total(self, cr, uid, ids, name, args, context=None):
res = {}
for record in self.browse(cr, uid, ids, context=context):
res['totalhour'] = record.hour_from + record.hour_to
return res
class hr_analytic_timesheet(osv.osv):
_name = "hr.analytic.timesheet"
_inherit = "hr.analytic.timesheet"
_columns = {
'hour_from' : fields.float('Work from', required=True, help="Start and End time of working.", select=True),
'hour_to' : fields.float("Work to", required=True),
'totalhour' : fields.function(_total, type='float', method=True, string='Total Hour'),
}
hr_analytic_timesheet()
我的xml:
<record id="view_ov_perf_timesheet_line_tree" model="ir.ui.view">
<field name="name">hr.analytic.timesheet.tree</field>
<field name="model">hr.analytic.timesheet</field>
<field name="inherit_id" ref="hr_timesheet.hr_timesheet_line_tree"/>
<field name="arch" type="xml">
<field name="unit_amount" position="replace">
<field name="hour_from" widget="float_time" string="Heure début"/>
<field name="hour_to" widget="float_time" string="Heure fin" />
<field name="totalhour" widget="float_time"/>
</field>
</field>
</record>
当我想添加或编辑一行时,我遇到了这个错误:
File "C:\Program Files\OpenERP 7.0-20130205-000102\Server\server\.\openerp\osv\orm.py", line 3729, in _read_flat
KeyError: 53
你能帮我一下吗?
好吧,这是我的代码:
def _total(self, cr, uid, ids, name, args, context=None):
res = {}
for record in self.browse(cr, uid, ids, context=context):
res['totalhour'] = record.hour_from + record.hour_to
return res
class hr_analytic_timesheet(osv.osv):
_name = "hr.analytic.timesheet"
_inherit = "hr.analytic.timesheet"
_columns = {
'hour_from' : fields.float('Work from', required=True, help="Start and End time of working.", select=True),
'hour_to' : fields.float("Work to", required=True),
'totalhour' : fields.function(_total, type='float', method=True, string='Total Hour'),
}
hr_analytic_timesheet()
我的xml:
<record id="view_ov_perf_timesheet_line_tree" model="ir.ui.view">
<field name="name">hr.analytic.timesheet.tree</field>
<field name="model">hr.analytic.timesheet</field>
<field name="inherit_id" ref="hr_timesheet.hr_timesheet_line_tree"/>
<field name="arch" type="xml">
<field name="unit_amount" position="replace">
<field name="hour_from" widget="float_time" string="Heure début"/>
<field name="hour_to" widget="float_time" string="Heure fin" />
<field name="totalhour" widget="float_time"/>
</field>
</field>
</record>
当我想添加或编辑一行时,我遇到了这个错误:
File "C:\Program Files\OpenERP 7.0-20130205-000102\Server\server\.\openerp\osv\orm.py", line 3729, in _read_flat
KeyError: 53
2 个回答
0
你可以这样定义你的函数:
def _total(self, cr, uid, ids, name, args, context=None):
res = {}
for record in self.browse(cr, uid, ids, context=context):
res[record.id] = record.hour_from + record.hour_to
return res
这里有一个链接,教你如何定义功能字段,希望对你有帮助。
http://doc.openerp.com/trunk/developers/server/03_module_dev_02/
2
你可以这样定义你的函数,然后再检查一下:
def _total(self, cr, uid, ids, name, args, context=None):
res = {}
for record in self.browse(cr, uid, ids, context=context):
res[record.id] = record.hour_from + record.hour_to
return res
或者这样定义:
def _total(self, cr, uid, ids, name, args, context=None):
res = {}
for record in self.browse(cr, uid, ids, context=context):
res[record.id] = {'totalhour' : 0.0}
res[record.id]['totalhour'] = record.hour_from + record.hour_to
return res
祝好,