Odoo/Python:模块依赖出错了?

Posted

技术标签:

【中文标题】Odoo/Python:模块依赖出错了?【英文标题】:Odoo/Python: modules dependencies gone wrong? 【发布时间】:2016-09-24 12:23:17 【问题描述】:

我有这些模块:

base_module, module_1 base_module_extend, module_2.

这些模块有这样的依赖关系:

module_1 取决于 base_modulebase_module_extend 依赖于 base_modulemodule_2 依赖于 base_module_extend

如果module_1module_2 没有同时安装,那么一切正常,但如果安装了它们,那么依赖关系会按照这些模块的安装顺序发生变化。

例如,如果我安装module_2,然后安装module_1,当我在该模块代码中调用super 时开始使用module_2,它会找到在base_module 中定义的类而不是@987654338先@@,然后有些东西坏了,因为它没有找到base_module_extend中定义的一些属性/参数。

如果我以相反的顺序安装:首先安装module_1module_2,然后当我使用module_2 时,它工作正常,因为它从super 调用正确的类。

有没有办法让这些模块调用正确的父类,如果它们都安装而不关心它们的安装顺序?

或者它是限制,你无能为力?..

更新

如果您以错误的顺序安装会损坏的示例(第一种情况):

这是base_module中定义的方法:

def export_data(self, export_time=fields.Datetime.now()):
    """
    This method can be overridden to implement custom data exportation
    """
    self.ensure_one()
    res = []
    return res

这是module_1中更改的方法:

def export_data(self, export_time=fields.Datetime.now()):
    res = super(SFTPImportExport, self).export_data(export_time)
    if self.process == 'custom_qty_available':
        res = self._export_qty_available(export_time)
    elif self.process == 'custom_sale':
        res = self._export_sale(export_time)
    return res

这是base_module_extend中更改的方法:

def export_data(
        self, export_time=fields.Datetime.now(), external_data=False):
    """
    Overrides export_data to be able to provide external_data that
    is already generated and can be directly returned to be used in export
    @param export_time: time of export
    @param external_data: used to bypass specific export
    method and directly use data provided
    """
    res = super(SFTPImportExport, self).export_data(export_time)

    return res

这是在module_2中更改的相同方法:

def export_data(self, export_time=fields.Datetime.now(), external_data=False):
    """
    Adds support for custom_invoice process
    """
    res = super(SFTPImportExport, self).export_data(export_time=export_time, external_data=external_data)
    if self.process == 'custom_invoice':
        res = external_data
    return res

更新2

当最后一个方法版本(module_2 中定义的那个)被执行时,我得到这个错误:

"/opt/odoo/projects/project/sftp_import_export_extend/models/sftp_import_export.py", line 47, in export_sftp

    export_time=now, external_data=external_data)

ValueError: "export_data() got an unexpected keyword argument 'external_data'" while evaluating

u'invoice_validate()'

所以看起来它得到了错误的父类,没有这样的关键字参数的那个

【问题讨论】:

我想阅读这些模块的代码,因为有时是函数被覆盖的方式,在您的示例中,如果在 4 个模块中定义了同名的函数,则所有 4 个应该运行(实际上,如果它是 on_change 或依赖于视图的东西,它可能会改变)。 我已经用方法更新了我的问题。该方法不依赖于视图。 很奇怪的情况,我不喜欢使用层次结构,因为 external_data 不在前两个模块上,无论如何它应该可以工作,在调用函数之前尝试登录 module_2 super(SFTPImportExport, self)看看它是什么样的对象/模型。 你能给我们更多的代码吗?我认为你正在继承,继承所做的是改变超级中的东西,所以我们可以从子级向父级添加功能,所以在第一种情况下,你是从模型模块 2 然后模块 1 设置功能,模块 1 是覆盖模块 2 -s 具有相同名称的函数。并且是第二种情况,我认为您正在使功能重载,这就是它起作用的原因。我不太确定我的答案,但也许它会对你有所帮助 【参考方案1】:

super() 在 Python 中倾向于选择错误(意外)的父级。作为替代方案,您可以使用从所需超类直接调用 Python 函数,而不是使用 super(),这是更可靠的方法。

我不知道您的 Odoo 模块的目录结构和源文件名,所以我会尝试大致猜测结构并描述应该做什么。请根据您的情况调整它(具有正确的模块名称、源文件名称等)。

如果所需的python类,比如命名为SFTPImportExport,包含所需的export_data方法,则在base_module_extend Odoo模块(相对路径)内的以下源文件中定义:base_module_extend/models/somefile.py,而不是使用super(),可以直接这样调用:

导入所需的超类:

from openerp.base_module_extend.models.somefile import SFTPImportExport as ParentSFTPImportExport 

并直接调用它的方法:

ParentSFTPImportExport.export_data(self, export_time=export_time, external_data=external_data)

在这种情况下,您需要的确切函数将被调用,因为您直接引用它。我建议做同样的事情,即在两个模块中直接调用所需的函数,以确保它们永远不会混合它们的目标函数。

【讨论】:

以上是关于Odoo/Python:模块依赖出错了?的主要内容,如果未能解决你的问题,请参考以下文章

导入带有可选链接的模块时出错

idea中maven编译出错问题

从 JBoss 7.1 引用依赖 Jar 文件时出错

运行 pod install 时出错:`UMFileSystemInterface` 依赖于`EXAV`

pytho xlrd简介

pytho常用模块2——random