目前odoo入坑开发中,在odoo中遇到的一些几行代码就能达到效果的一些操作,和在源码中看到的都记下来了。
以下:
- 直接在页面点击,就能动态显示和隐藏对应字段 (我知道能用@api.depends啊 onchange啊,但是又更简单的实现)
对,就这样就够了,用attrs。重点是自动显隐
<field name="on_change"/>
<field name="probability" attrs="{'invisible': [('on_change', '=', False)]}"/>
- tree列表手动排序 只需要在xml里加个挂件。*widget="handle"*
<tree string="Stages">
<field name="sequence" widget="handle"/>
<field name="name"/>
...
</tree>
- 在页面中鼠标hover按钮时切换显示文字 (active->archive)
使用option terminology属性
<field name="active" widget="boolean_button" options='{"terminology": "archive"}'/>
-
直接用create直接创建对应字段多条记录
文档:
Tuple Effect
(0, 0, dict_val) Create a new record that will be related to the main record
(6, 0, id_list) Create a relation between the record being created and existing
records, whose IDs are in the Python list id_list
Caution: When used on a One2many, this will remove the records
from any previous relation
① **one2many字段**
lists = [
(0,0,{'c1':xxx,'c2':xx}),
(0,0,{'c1':xxx,'c2':xx}),
(0,0,{'c1':xxx,'c2':xx}),
]
# A.a model
# 这样就直接用lists的数据创建了多条A.c模型的记录,并将这些记录与A.b模型的c.ids关联了起来
# 需要注意的是lists里面的键要与A.c模型的字段吻合,A.b,A.c之间的字段对应关系也要写好
# 可以说是超级好用了
self.env['A.a'].sudo().create({'c_ids':lists})
# A.b model
c_ids = fields.One2many('A.c', 'b_id', string=u"xxxx")
# A.c model
c1 = fields.char('这是对应的字段1')
c2 = fields.char('这是对应的字段2')
b_id = fields.Many2one('A.b', string=u"xxxxx", ondelete='cascade')
②**many2many字段的数据写入**
# 注意的是,relation_partners的格式,见上面文档(id_list)
# A.a 模型
relation_partners = self.relation_ids.mapped('partner_id.id')
val = { 'relation_ids': [( 6 , 0, relation_partners)] }
self.env['A.b'].sudo().create(val)
# A.b模型 对应的字段,xml
relation_ids=fields.Many2many('res.partner','loanrecord_partner_rel',
'a_id','b_id',string=u'关系人')
<field name="relation_ids" widget="many2many_kanban" options="{'not_delete': True}">