1

我正在尝试在 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
4

2 回答 2

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

问候,

于 2013-02-28T10:08:52.763 回答
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/

于 2013-02-27T05:05:49.250 回答