odoo 关系字段(关联关系)
Many-to-one关联
publisher_id = fields.Many2one(comodel_name= 'res.partner', domain='',context={},ondelete='',auto_join='',delegate='',string='Publisher')
many-to-one模型字段在数据表中创建一个字段,并带有指向关联表的外键,其中为关联记录的数据库 ID。以下是many-to-one字段可用的关键字参数:
- ondelete定义关联记录删除时执行的操作:context是一个数据字典,可在浏览关联时为网页客户端传递信息,比如设置默认值。
- set null (默认值): 关联字段删除时会置为空值
- restricted:抛出错误阻止删除
- cascade:在关联记录删除时同时删除当前记录
- domain是一个域表达式:使用一个元组列表过滤记录来作为关联记录选项。
- auto_join=True允许ORM在使用关联进行搜索时使用SQL连接。使用时会跳过访问安全规则,用户可以访问安全规则不允许其访问的关联记录,但这样 SQL 的查询会更有效率且更快。
- delegate=True 创建一个关联记录的代理继承。使用时必须设置required=True和ondelete=’cascade’
One-to-many反向关联
published_book_ids = fields.One2many(
comodel_name= 'library.book', # related model
Many-to-many关联
author_ids = fields.Many2many(
class Many2many(_RelationalMulti):
""" Many2many field; the value of such a field is the recordset.
:param comodel_name: name of the target model (string)
The attribute ``comodel_name`` is mandatory except in the case of related
fields or field extensions.
:param relation: optional name of the table that stores the relation in
the database (string)
:param column1: optional name of the column referring to "these" records
in the table ``relation`` (string)
:param 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!
:param domain: an optional domain to set on candidate values on the
client side (domain or string)
:param context: an optional context to use on the client side when
handling that field (dictionary)
:param limit: optional limit to use upon read (integer)
"""