如何在另一个模块中复制一个模块的值 [ODOO]
How to copy value of a module in another module [ODOO]
我做了一个自定义订阅模块
subscription.py
from openerp.osv import fields, osv
class subscriptions(osv.osv):
_name = "subscriptions.sub"
_description = "subscriptions"
_columns = {
'sub_id': fields.integer('Subscription ID', size=10),
'cust_id': fields.many2one('res.partner','customer_id', 'Customer ID')
}
partner.py
from openerp import fields, models
class Partner(models.Model):
_inherit = 'res.partner'
customer_id = fields.Integer('Customer ID'),
subscription_id = fields.One2many('subscriptions.sub', 'sub_id', 'Subscription ID')
当我从我的客户模块创建订阅时,它也会显示在订阅模块中,但是当我在我的订阅模块中创建订阅时,它不会显示在客户模块中。
我能在正确的方向上得到一些帮助吗?
您的问题是关于理解传递给 Many2one 和 One2many 字段的参数。
对于 Many2one
在documentation中,当前声明如下:
class openerp.fields.Many2one(comodel_name=None, string=None, **kwargs)
The value of such a field is a recordset of size 0 (no record) or 1 (a single record).
Parameters
comodel_name
-- name of the target model (string)
auto_join
-- whether JOINs are generated upon search through that field (boolean, by default False
)
The attribute comodel_name
is mandatory except in the case of related fields or field extensions.
因此,fields.Many2one
的第二个参数是该字段在界面中的名称。在旧的 API 中我们可以看到 (in openerp/osv/fields.py) 第三个参数是一个 auto_join
标志:
class many2one(_column):
# ...
def __init__(self, obj, string='unknown', auto_join=False, **args):
因此,在您给出的示例中,您应该删除第二个参数,并且在指定 many2one 时只有两个参数。
对于一对多
对于 One2many,文档指出:
class openerp.fields.One2many(comodel_name=None, inverse_name=None, string=None, **kwargs)
One2many field; the value of such a field is the recordset of all the records in comodel_name
such that the field inverse_name
is equal to the current record.
Parameters
comodel_name
-- name of the target model (string)
inverse_name
-- name of the inverse Many2one
field in comodel_name
(string)
The attributes comodel_name
and inverse_name
are mandatory except in the case of related fields or field extensions.
所以在这种情况下,您认为需要第二个参数是对的,但是您选择的参数 (sub_id
) 是错误的。对于 inverse_name
,您必须在 comodel_name
上选择一个 Many2one
字段,它将指向带有 One2many
的模型。因此在你的例子中 inverse_name
应该是 cust_id
.
结论
要建立从模型的 Many2one
字段到另一个模型的 One2many
字段的关系,One2many
字段的 inverse_name
必须是Many2One
字段的技术名称,例如:
class ModelA(models.Model):
_name = 'model.a'
m2o_model_a = fields.Many2one('model.a')
class ModelB(models.Model):
_name = 'model.b'
# inverse_name is set to the Many2one of ModelA which points to ModelB
o2m_model_a = fields.One2many('model.b', 'm2o_model_a')
如果您的代码不遵循这一点,则不会在两个字段之间建立 link,您将拥有每个模型字段的独立值。
我做了一个自定义订阅模块
subscription.py
from openerp.osv import fields, osv
class subscriptions(osv.osv):
_name = "subscriptions.sub"
_description = "subscriptions"
_columns = {
'sub_id': fields.integer('Subscription ID', size=10),
'cust_id': fields.many2one('res.partner','customer_id', 'Customer ID')
}
partner.py
from openerp import fields, models
class Partner(models.Model):
_inherit = 'res.partner'
customer_id = fields.Integer('Customer ID'),
subscription_id = fields.One2many('subscriptions.sub', 'sub_id', 'Subscription ID')
当我从我的客户模块创建订阅时,它也会显示在订阅模块中,但是当我在我的订阅模块中创建订阅时,它不会显示在客户模块中。
我能在正确的方向上得到一些帮助吗?
您的问题是关于理解传递给 Many2one 和 One2many 字段的参数。
对于 Many2one
在documentation中,当前声明如下:
class openerp.fields.Many2one(comodel_name=None, string=None, **kwargs)
The value of such a field is a recordset of size 0 (no record) or 1 (a single record).
Parameters
comodel_name
-- name of the target model (string)auto_join
-- whether JOINs are generated upon search through that field (boolean, by defaultFalse
)The attribute
comodel_name
is mandatory except in the case of related fields or field extensions.
因此,fields.Many2one
的第二个参数是该字段在界面中的名称。在旧的 API 中我们可以看到 (in openerp/osv/fields.py) 第三个参数是一个 auto_join
标志:
class many2one(_column):
# ...
def __init__(self, obj, string='unknown', auto_join=False, **args):
因此,在您给出的示例中,您应该删除第二个参数,并且在指定 many2one 时只有两个参数。
对于一对多
对于 One2many,文档指出:
class openerp.fields.One2many(comodel_name=None, inverse_name=None, string=None, **kwargs)
One2many field; the value of such a field is the recordset of all the records in
comodel_name
such that the fieldinverse_name
is equal to the current record.Parameters
comodel_name
-- name of the target model (string)inverse_name
-- name of the inverseMany2one
field incomodel_name
(string)The attributes
comodel_name
andinverse_name
are mandatory except in the case of related fields or field extensions.
所以在这种情况下,您认为需要第二个参数是对的,但是您选择的参数 (sub_id
) 是错误的。对于 inverse_name
,您必须在 comodel_name
上选择一个 Many2one
字段,它将指向带有 One2many
的模型。因此在你的例子中 inverse_name
应该是 cust_id
.
结论
要建立从模型的 Many2one
字段到另一个模型的 One2many
字段的关系,One2many
字段的 inverse_name
必须是Many2One
字段的技术名称,例如:
class ModelA(models.Model):
_name = 'model.a'
m2o_model_a = fields.Many2one('model.a')
class ModelB(models.Model):
_name = 'model.b'
# inverse_name is set to the Many2one of ModelA which points to ModelB
o2m_model_a = fields.One2many('model.b', 'm2o_model_a')
如果您的代码不遵循这一点,则不会在两个字段之间建立 link,您将拥有每个模型字段的独立值。