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进行自己想要的调整。
  • 有问题的小伙伴可以在下方留言,或许我可以帮助到你。
posted @ 2021-07-20 21:04  FANDX  阅读(382)  评论(0编辑  收藏  举报