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)
"""