gpt4 book ai didi

python - 如何在openERP 7中添加功能?

转载 作者:行者123 更新时间:2023-12-01 05:48:54 24 4
gpt4 key购买 nike

我试图在 openERP 版本 7 中创建一个新模块。在我的类(class)中,我有以下代码:

_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_fromhour_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

最佳答案

你能像这样定义你的函数并再次检查吗:

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

问候,

关于python - 如何在openERP 7中添加功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15102388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com