继承函数odoo python

时间:2016-09-07 09:28:30

标签: python openerp

我想继承模块'hr_holidays'中的函数来计算函数的剩余叶子:

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

我创建了自己的模块,继承到hr_holidays并尝试使用此代码继承,但它不起作用

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

请帮帮我

4 个答案:

答案 0 :(得分:0)

你必须在super()中调用正确的类:

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

答案 1 :(得分:0)

您需要使用HrHolidays调用super,并将name和args传递给_get_remaining_days方法并覆盖remaining_leaves字段:

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

答案 2 :(得分:0)

感谢所有人, 但是我在这个领域遇到了问题,我想把我的功能授予我的领域我已经做到了但是没有工作:

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

我收到了这个错误 enter image description here

答案 3 :(得分:0)

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

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