Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
1 Vastaa
3557 Näkymät

Hi,

I have created a function to be called by multiple class within the same file(.py)

But How?

The following is my file.py

------------------------

    def holiday(lookup_date):
    # check holidays
    
    hday_credit_rate = 0
 
*****Error below calling the table outside the class****** 
holidays = env['hr.company.holidays'].sudo().search([('date', '=',                     lookup_date)])

    if holidays:
    holiday_rate = env['hr.holiday.rate'].sudo().search([('id', '=',                             holidays.holiday_rate_id.id)])
    hday_credit_rate = holidays.credited_rate

return hday_credit_rate


class dtr(models.Model):
_name = 'hr.dtr'
_inherit = ['mail.thread', 'mail.activity.mixin']
_description = "Daily Time Record"

employee_id = fields.Many2one('hr.employee', string='Employee', required=True)

********start to call the table outside the class
holiday_credit = holiday('2020/07/03')    



Avatar
Hylkää
Paras vastaus

Hello gain

If you want to call method from multiple classes in same .py file then declare method as you did above and then you can call method with help of any instance(Object) method.

Example:

def holiday(lookup_date):
    # check holidays
    hday_credit_rate = 0
    *****Error below calling the table outside the class****** 
    holidays = env['hr.company.holidays'].sudo().search([('date', '=',lookup_date)])
    if holidays:
        holiday_rate = env['hr.holiday.rate'].sudo().search([('id', '=',holidays.holiday_rate_id.id)])
        hday_credit_rate = holidays.credited_rate
    return hday_credit_rate
class dtr(models.Model):
    _name = 'hr.dtr'
    _inherit = ['mail.thread', 'mail.activity.mixin']
    _description = "Daily Time Record"
    employee_id = fields.Many2one('hr.employee', string='Employee', required=True)
    ********start to call the table outside the class 
    def get_holiday_credit(self):
        holiday_credit = holiday('2020/07/03')
 
 
 
You can call this method from multiple classes And if you have a method and you want to call it  from different files you can just define method in one file and then import that file in required file to call your method.

Thanks

Avatar
Hylkää
Tekijä

Hi Hussain,

def get_holiday_credit(self) is not visible in DTR(class). because its calling Holiday(function) which is outside of class.

CODE:

Aiheeseen liittyviä artikkeleita Vastaukset Näkymät Toimenpide
1
kesäk. 20
3880
0
helmik. 19
5179
0
lokak. 20
3353
2
jouluk. 23
14903
0
lokak. 23
33