Odoo ORM研究1 - BaseModel中的類屬性的作用

概述

我們在寫odoo項目的時候,經常繼承model.Model來創建我們自己的ORM映射關係表。

AbstractModel = BaseModel
# 源碼
class Model(AbstractModel):
    _auto = True                # automatically create database backend
    _register = False           # not visible in ORM registry, meant to be python-inherited only
    _abstract = False           # not abstract
    _transient = False          # not transient

這裏發現我們繼承的Model其實是繼承AbstractModel,而AbstractModel是等於BaseModel的,所以我們今天就來研究一下BaseModel做了什麼工作。

先研究一下所有類屬性最終做了什麼工作


# 這個很好理解,是否創建數據表,默認我們常用的繼承的Model已經將默認值設爲True,如果你只是想創建基礎類讓自己別的類來繼承,那麼你就可以創建繼承AbstractModel來進行實現。
_auto = False

# 註冊可見性(具體還沒有測試使用過)。
_register = False           

# 是否是抽象類(Model爲False)。
_abstract = True

# 是否有時效性,當爲True的時候,存儲的數據過一段時間會消失,這裏我們可以繼承TransientModel實現這個效果。
_transient = False

# 數據表的名稱。
_name = None  

# 數據表的描述信息。
_description = None         

# 是否僅適用於自定義模型(沒測試過)。
_custom = False            

# 繼承表,如果沒有_name,那麼則直接在主表中添加字段;
# 如果有_name,那麼則會把父類的所有的字段拿過來創建一張新的表。
_inherit = None

"""
_inherits = {
          'a.model': 'a_field_id',  # a_field_id字段必須是many2one的字段
          'b.model': 'b_field_id'
      }
可以直接指定當前表的字段是否關聯到父表;
這樣的繼承方式,可以直接使用主表的字段和方法,相當於在外鍵的同時會自動創建外鍵字段表中的數據。
"""
_inherits = {}

# 當指定_table的時候,那麼在數據庫就會創建這個_table的名稱,但是在ORM中使用env查詢還是使用_name的名稱值來作爲參考。
_table = None            

# 還未具體使用,應該是table做query的時候會用到。
_table_query = None        

# 給指定的字段添加作爲排序字段。
_sequence = None            

# 給SQL加上約束
_sql_constraints = []       

# 在外鍵的字段時候會顯示的display_name的字段,這個字段可以自由改動自己想要顯示的值
_rec_name = None          

# 默認排序的字段
_order = 'id'    

# 下面都是一些還沒有做研究的字段
_parent_name = 'parent_id'  #: the many2one field used as parent field
_parent_store = False
"""set to True to compute parent_path field.

    Alongside a :attr:`~.parent_path` field, sets up an indexed storage
    of the tree structure of records, to enable faster hierarchical queries
    on the records of the current model using the ``child_of`` and
    ``parent_of`` domain operators.
    """
_active_name = None         #: field to use for active records
_date_name = 'date'         #: field to use for default calendar view
_fold_name = 'fold'         #: field to determine folded groups in kanban views

_needaction = False         # whether the model supports "need actions" (Old API)
_translate = True           # False disables translations export for this model (Old API)
_check_company_auto = False
"""On write and create, call ``_check_company`` to ensure companies
    consistency on the relational fields having ``check_company=True``
    as attribute.
    """

_depends = {}
"""dependencies of models backed up by SQL views
    ``{model_name: field_names}``, where ``field_names`` is an iterable.
    This is only used to determine the changes to flush to database before
    executing ``search()`` or ``read_group()``. It won't be used for cache
    invalidation or recomputing fields.
    """

總結

  • odoo ORM中類屬性的改變可以讓odoo的model做出很大的改動(這些屬性都是父類屬性可以在繼承的時候可以重寫這些屬性)。
  • 下一章將繼續研究odoo orm中的一些內置方法,讓我們學習了之後更加靈活的對odoo ORM進行自己想要的調整。
  • 有問題的小夥伴可以在下方留言,或許我可以幫助到你。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章