概述
我們在寫odoo項目的時候,經(jīng)常繼承model.Model來創(chuàng)建我們自己的ORM映射關(guān)系表。
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
這里發(fā)現(xiàn)我們繼承的Model其實是繼承AbstractModel,而AbstractModel是等于BaseModel的峡蟋,所以我們今天就來研究一下BaseModel做了什么工作坟桅。
先研究一下所有類屬性最終做了什么工作
# 這個很好理解华望,是否創(chuàng)建數(shù)據(jù)表,默認(rèn)我們常用的繼承的Model已經(jīng)將默認(rèn)值設(shè)為True,如果你只是想創(chuàng)建基礎(chǔ)類讓自己別的類來繼承仅乓,那么你就可以創(chuàng)建繼承AbstractModel來進(jìn)行實現(xiàn)赖舟。
_auto = False
# 注冊可見性(具體還沒有測試使用過)。
_register = False
# 是否是抽象類(Model為False)夸楣。
_abstract = True
# 是否有時效性宾抓,當(dāng)為True的時候,存儲的數(shù)據(jù)過一段時間會消失豫喧,這里我們可以繼承TransientModel實現(xiàn)這個效果石洗。
_transient = False
# 數(shù)據(jù)表的名稱。
_name = None
# 數(shù)據(jù)表的描述信息紧显。
_description = None
# 是否僅適用于自定義模型(沒測試過)讲衫。
_custom = False
# 繼承表,如果沒有_name孵班,那么則直接在主表中添加字段涉兽;
# 如果有_name招驴,那么則會把父類的所有的字段拿過來創(chuàng)建一張新的表。
_inherit = None
"""
_inherits = {
'a.model': 'a_field_id', # a_field_id字段必須是many2one的字段
'b.model': 'b_field_id'
}
可以直接指定當(dāng)前表的字段是否關(guān)聯(lián)到父表枷畏;
這樣的繼承方式别厘,可以直接使用主表的字段和方法,相當(dāng)于在外鍵的同時會自動創(chuàng)建外鍵字段表中的數(shù)據(jù)拥诡。
"""
_inherits = {}
# 當(dāng)指定_table的時候触趴,那么在數(shù)據(jù)庫就會創(chuàng)建這個_table的名稱,但是在ORM中使用env查詢還是使用_name的名稱值來作為參考袋倔。
_table = None
# 還未具體使用雕蔽,應(yīng)該是table做query的時候會用到。
_table_query = None
# 給指定的字段添加作為排序字段宾娜。
_sequence = None
# 給SQL加上約束
_sql_constraints = []
# 在外鍵的字段時候會顯示的display_name的字段批狐,這個字段可以自由改動自己想要顯示的值
_rec_name = None
# 默認(rèn)排序的字段
_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.
"""
總結(jié)
- odoo ORM中類屬性的改變可以讓odoo的model做出很大的改動(這些屬性都是父類屬性可以在繼承的時候可以重寫這些屬性)。
- 下一章將繼續(xù)研究odoo orm中的一些內(nèi)置方法前塔,讓我們學(xué)習(xí)了之后更加靈活的對odoo ORM進(jìn)行自己想要的調(diào)整嚣艇。
- 有問題的小伙伴可以在下方留言,或許我可以幫助到你华弓。