How add a function in openERP 7?

2020-07-30 02:27发布

问题:

I was trying create a new module in openERP version 7. In my class I have this code :

_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"),
}

I don't find any solution to add a function in my class. The function which I need it return the sum of hour_from and hour_to. Can anyone help?

I try this code before declaring my _columns :

def _total(self, cr, uid, ids, name, args, context=None):

  res = {}

    res['totalhour'] = hour_from + hour_to

return res

When I restart the server I receive this error:

No handler found.

(Update from other post)

Well this is my code:

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()

My 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>

When I want to add or edit a line, I have this error:

File "C:\Program Files\OpenERP 7.0-20130205-000102\Server\server\.\openerp\osv\orm.py", line 3729, in _read_flat
KeyError: 53

can you help mee pleese


wel this is my code:

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()

My 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>

when i want to add or redit a line i have this error:

File "C:\Program Files\OpenERP 7.0-20130205-000102\Server\server\.\openerp\osv\orm.py", line 3729, in _read_flat
KeyError: 53

回答1:

Can you define you function like this and check again:

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

or like this:

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

Regards,



回答2:

You can define your function like this:

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

Here is the link for how to define functional fields, Hope this helpful for you.

http://doc.openerp.com/trunk/developers/server/03_module_dev_02/