【发布时间】:2016-09-24 12:23:17
【问题描述】:
我有这些模块:
base_module, module_1
base_module_extend, module_2.
这些模块有这样的依赖关系:
-
module_1取决于base_module。 -
base_module_extend依赖于base_module。 -
module_2依赖于base_module_extend。
如果module_1 和module_2 没有同时安装,那么一切正常,但如果安装了它们,那么依赖关系会按照这些模块的安装顺序发生变化。
例如,如果我安装module_2,然后安装module_1,当我在该模块代码中调用super 时开始使用module_2,它会找到在base_module 中定义的类而不是@987654338先@@,然后有些东西坏了,因为它没有找到base_module_extend中定义的一些属性/参数。
如果我以相反的顺序安装:首先安装module_1 和module_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 具有相同名称的函数。并且是第二种情况,我认为您正在使功能重载,这就是它起作用的原因。我不太确定我的答案,但也许它会对你有所帮助
标签: python python-2.7 dependencies openerp python-module