如何在odoo中创建关系字段?

How to create a relation fields in odoo?

关于如何在 openerp 中创建关系字段,如 one2many、many2many、many2one 和 one2one,没有合适的示例。那么谁能给我看一下销售模块的例子。

我没有销售模块的具体示例。但是你可以从下面得到一般的想法 syntax.It 对任何类型的应用程序都有帮助。

class openerp.fields.Many2one(comodel_name=None, string=None, **kwargs)

基地:openerp.fields._Relational

此类字段的值是大小为 0(无记录)或 1(单条记录)的记录集。 参数

comodel_name -- name of the target model (string)
domain -- an optional domain to set on candidate values on the client side (domain or string)
context -- an optional context to use on the client side when handling that field (dictionary)
ondelete -- what to do when the referred record is deleted; possible values are: 'set null', 'restrict', 'cascade'
auto_join -- whether JOINs are generated upon search through that field (boolean, by default False)
delegate -- set it to True to make fields of the target model accessible from the current model (corresponds to _inherits)

属性 comodel_name 是强制性的,除非是相关字段或字段扩展。

============================================= ==========================

class openerp.fields.One2many(comodel_name=None, inverse_name=None, string=None, **kwargs)

碱基:openerp.fields._Relational多

一对多字段;此类字段的值是 comodel_name 中所有记录的记录集,使得字段 inverse_name 等于当前记录。

Parameters  
comodel_name -- name of the target model (string)
inverse_name -- name of the inverse Many2one field in comodel_name     (string)
domain -- an optional domain to set on candidate values on the client  side (domain or string)
context -- an optional context to use on the client side when handling that field (dictionary)
auto_join -- whether JOINs are generated upon search through that  field (boolean, by default False)
limit -- optional limit to use upon read (integer)

属性 comodel_name 和 inverse_name 是强制性的,除非是相关字段或字段扩展。

============================================= =============================

class openerp.fields.Many2many(comodel_name=None, relation=None, column1=None, column2=None, string=None, **kwargs)

碱基:openerp.fields._Relational多

多对多字段;这样一个字段的值就是记录集。

参数-------------------------------------------- --------------------

comodel_name -- name of the target model (string)

属性 comodel_name 是强制性的,除非是相关字段或字段扩展。

参数

relation -- optional name of the table that stores the relation in the database (string)
column1 -- optional name of the column referring to "these" records in the table relation (string)
column2 -- optional name of the column referring to "those" records in the table relation (string)
The attributes relation, column1 and column2 are optional. If not given, names are automatically generated from model names, provided model_name and comodel_name are different!

参数

domain -- an optional domain to set on candidate values on the client side (domain or string)
context -- an optional context to use on the client side when handling that field (dictionary)
limit -- optional limit to use upon read (integer)

可能对你有帮助,

_columns = {
'current_rate': fields.related('company_currency_id','rate_silent', type='float', relation='res.currency',digits_compute=dp.get_precision( 'Account'), string='Current Rate', readonly=True),
}

这里,

company_currency_id => 新字段关联的同一模型中的字段,

rate_silent => 是您要与新字段关联的字段,表示来自源模型的字段,

relation => 是源模型名称,

type => 是源字段的数据类型

Note : When you update value in your newly defined related field it will be updated in source field as well, though it's always advisable to set readonly in newly defined field.