如何从Odoo中的纯Python类继承来修改方法?

如何从Odoo中的纯Python类继承来修改方法?,python,python-2.7,odoo-8,odoo,Python,Python 2.7,Odoo 8,Odoo,我必须修改Odoo中的一个方法。问题是,包含该方法的类没有像往常一样声明(它没有使用odooapi),因此我不知道如何模拟odooapi的\u inherit参数 这是包含该方法的类(模块是OCA提供的帐户\财务\报告\ webkit): 我想修改的方法是这个(它在CommonReportHeaderWebkit类中): 为了覆盖它,我在自定义模块中进行了猴子补丁: from openerp.addons.account_financial_report_webkit.report.common

我必须修改Odoo中的一个方法。问题是,包含该方法的类没有像往常一样声明(它没有使用odooapi),因此我不知道如何模拟odooapi的
\u inherit
参数

这是包含该方法的类(模块是OCA提供的
帐户\财务\报告\ webkit
):

我想修改的方法是这个(它在
CommonReportHeaderWebkit
类中):

为了覆盖它,我在自定义模块中进行了猴子补丁:

from openerp.addons.account_financial_report_webkit.report.common_reports \
    import CommonReportHeaderWebkit

def is_initial_balance_enabled(self, main_filter):
    if main_filter not in ('filter_no', 'filter_date', 'filter_period'):
        return False
    return True

CommonReportHeaderWebkit.is_initial_balance_enabled = is_initial_balance_enabled
这是正常的,但问题是这样我覆盖了整个方法,我想使用
super
,因为现在我必须对其他方法做同样的操作,我不能覆盖它的整个代码


有人知道如何正确地使用这个方法吗?

我不是python方面的专家,但据我所知,这个方法是可行的 用作对象,因此我认为这将起作用

来自openerp.addons.account\u financial\u report\u webkit.report.common\u reports\
导入CommonReportHeaderWebkit
#在丢失原始方法之前,请先保留对其的引用。
_super\u is\u initial\u balance\u enabled=CommonReportHeaderWebkit.is\u initial\u balance\u enabled
def已启用初始平衡(自、主过滤器):
#像超级高手一样执行它
返回\u超级\u初始\u平衡\u已启用(自身、主\u过滤器)
CommonReportHeaderWebkit.is_initial_balance_enabled=is_initial_balance_enabled

您的解决方案非常有效。我想有另一个更干净的解决方案,但我想我找不到了,可能没有比你更简单、更干净的解决方案了。非常感谢@Cherif。我想我们可能需要自己更新类,python支持多重继承。因此,如果修改该类,它将自己继承新类。我这么说是因为odoo也这么做了,他从所有的类中创建了一个类对象,包含了所有的字段和方法,我想我是在用python描绘一种多么强大的语言,你可以用它做任何你想做的事情。在运行时更新类是第二个解决方案,你不这么认为吗。
def is_initial_balance_enabled(self, main_filter):
    if main_filter not in ('filter_no', 'filter_year', 'filter_period'):
        return False
    return True
from openerp.addons.account_financial_report_webkit.report.common_reports \
    import CommonReportHeaderWebkit

def is_initial_balance_enabled(self, main_filter):
    if main_filter not in ('filter_no', 'filter_date', 'filter_period'):
        return False
    return True

CommonReportHeaderWebkit.is_initial_balance_enabled = is_initial_balance_enabled