1.related

字段=fields.类型(related=“某个字段.类字段”,
store=true/false)
例如,
user_id = fields.Many2one(
‘res.users’,
related=‘partner_id.user_id’,
string=‘Sales Representative’,
readonly=True, store=True)
related字段可以简记为“带出字段”,由当前模型的某个关联类型字段的某个字段带出值。
related使用时候,需要在相应的py文件字段上加readonly属性

2.active

odoo中默认,active字段为False的记录上不显示的。
a.通过odoo提供的搜索视图来实现:
自定义筛选——active字段——为假
b.打开debug,向动作context中添加 {‘active_test’: False}

odoo res_partner的many2one字段 auto_join自动为ture了 odoo related字段_xml


或者在后端xml代码中,加入。


{‘active_test’: True}

3.xml中context用法

1)模型有多个同种视图时,指定打开具体的视图

{‘tree_view_ref’:‘模块.view_tree_XXX’,
‘form_view_ref’:‘模块.view_form_XXX’}

<field name=“search_view_id” ref="view_search_XXX/>

2)在跳转的同时启用过滤器

{‘search_default_过滤器名’: [active_id]/True}

3)传递数据,可用于domain中作为表达式的值
{“key”:value}

4)指定跳转过去的视图记录的分组方式

{‘group_by’: [‘字段’,‘…’];‘group_by_no_leaf’:1}

4.列隐藏

column_invisible 只有在odoo12及其以上才可使用

<field name="Approve_Date" attrs="{'column_invisible':[('parent.Department','=','HRP')]}"/>

5.tree显示条数

<tree limit=200> 
	<field name="name"/> 
</tree>
<xpath expr="//tree" position="attributes">     		<attribute name="limit">200</attribute> 
</xpath>

6.视图继承

ODOO支持的视图类型:form、tree、search …
支持的定位方法:见xpath定位方法

<xpath expr="//page[@name='name']" position="replace">
	<field name="name"/>
</xpath>

支持的规则:before、after、replace、inside、attributes
插入:
position=‘before’
position=“after”
替换:
position=“replace”
内部创建:
position=“inside”
修改属性:修改属性能够实现的功能,不要使用 replace
position=“attributes”

Odoo中model

模型对象Model

Odoo的模型对象在odoo模块的models.py文件中,最基础的对象是BaseModel;
Odoo的模型对象有三个:AbstractModel、Model、TransientModel

BaseModel

BaseModel是一切模型的基础

  • _auto = False 是否在后台创建数据表
  • _register = False
  • _abstract = True 是否是抽象模型
  • _transient = False 是否是临时模型
  • _name = None 模型名称
  • _description = None 模型描述
  • _custom = False 是否自定义模型
  • _inherit = None 被继承的模型名称
  • _inherits = {} 继承关系
  • table = None 模型对应的数据表的名称,默认值是_name的值中将.转换成
  • _sql_constraints = [] SQL约束
  • _rec_name = None 在其他模型中引用此模型时显示的字段,默认是name
  • _order = ‘id’ 记录排序
  • _parent_name = ‘parent_id’ 关联父级记录的字段
  • _parent_store = False 值为True时会计算parent_left和parent_right
  • _parent_order = False 父级记录排序字段
AbstractModel

AbstractModel 是一个抽象模型不会在数据库创建对应表,Model可以继承AbstractModel,AbstractModel为多个Model提供相同属性的统一声明

Model

Model继承自AbstractModel,但是Model的 _auto=True , _abstract = True ;
Model的模型对象在模块安装或升级的时候会自动在数据库中创建相应的数据表

TransientModel

TransientModel继承自Model,但是TransientModel的_transient = True,TransientModel是一种特殊的Model,TransientModel对应的数据表中的数据系统会定时的清理;TransientModel的数据只能做临时数据使用,一般向导对象模型会声明成TransientModel

–继续添加