Odoo ORM研究1 - BaseModel中的類屬性的作用
阿新 • • 發佈:2021-07-20
概述
我們在寫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進行自己想要的調整。
- 有問題的小夥伴可以在下方留言,或許我可以幫助到你。
本文 FANDX 原創內容,未經允許禁止轉發,違者必究其責任!