简体   繁体   中英

inherited function odoo python

i want to inherit function in module 'hr_holidays' that calculate remaining leaves the function is :

hr_holiday.py:

def _get_remaining_days(self, cr, uid, ids, name, args, context=None):
    cr.execute("""SELECT
            sum(h.number_of_days) as days,
            h.employee_id
        from
            hr_holidays h
            join hr_holidays_status s on (s.id=h.holiday_status_id)
        where
            h.state='validate' and
            s.limit=False and
            h.employee_id in %s
        group by h.employee_id""", (tuple(ids),))
    res = cr.dictfetchall()
    remaining = {}
    for r in res:
        remaining[r['employee_id']] = r['days']
    for employee_id in ids:
        if not remaining.get(employee_id):
            remaining[employee_id] = 0.0
    return remaining

i had create my own module that inherited to hr_holidays and try this code to inherit but it isnt work


myclass.py

class HrHolidays(models.Model):
    _inherit = 'hr.holidays'

    interim = fields.Many2one(
        'hr.employee',
        string="Interim")
    partner_id = fields.Many2one('res.partner', string="Customer")
    remaining_leaves = fields.Char(string='Leaves remaining')


    def _get_remaining_days(self, cr, uid, ids, name, args, context=None):
        res = super(hr_holidays,self)._get_remaining_days(cr, uid, ids, name, args, context)
        return res

please help me

You have to call the right Class in super():

res = super(HrHolidays, self)._get_remaining_days(
    cr, uid, ids, name, args, context)

You need to call super with HrHolidays and pass just name and args to _get_remaining_days method and override remaining_leaves field:

Python

class HrHolidays(models.Model):
_inherit = 'hr.employee'

@api.model
def _get_remaining_days(self):
    res = super(HrHolidays, self)._get_remaining_days(name='', args={})
    for record in self:
        if record.id in res:
            record.remaining_leaves = res.get(record.id)

    return res

remaining_leaves = fields.Float(compute='_get_remaining_days',
                    string='Remaining Legal Leaves')

thanks for all , but i got problem in the field ,i want to award my function to my field i have done that but isn't work :

    remaining_leaves = fields.Function(_get_remaining_days,string='Leaves')

i got this error 在此处输入图片说明

我认为该功能有效,但我无法通过fields.function显示它

   remaining_leaves = fields.Function(_get_remaining_days,string='Leaves')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM