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

       inverse_name= 'publisher_id', # fields for "this" on related model
      domain='',
      context={},
     auto_join='',
     limit=0,
        string='Published Books')
 

Many-to-many關聯

    author_ids = fields.Many2many(

    comodel_name='res.partner', # 關聯模型(必填)
    relation='library_book_res_partner_rel', # 關聯表名
    column1='a_id', # 本記錄關聯表字段
    column2='p_id', # 關聯記錄關聯表字段
    string='Authors') # string標簽文本
  
    

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)

"""

在創建抽象模型時,many-to-many中不要使用column1和column2屬性。在 ORM 設計中對抽象模型有一個限制,如果指定關聯表列名,就無法再被正常繼承。
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM