通过代码修改视图

在odoo中, 修改表单视图的传统方法是写xml文件, 继承原有的视图。

〈 xpath expr='//form[@string="Invoice"]' possition='attributes'〉

〈!-- Frist intent : nothing happened --〉

〈attribute name="edit" attrs="{'invisible:[('state','=','paid')]'}"/〉

<xpath expr='//form[@string="Process Line"]' position='attributes'>
<attribute name="attrs">{'colors':'gray:line_new == True;'}</attribute>
</xpath>

有些模块,比如sales.order,  经过了多次继承, 二次开发时再使用这种方法,会使系统结构更加复杂, 并且还可能与其他的继承方法发生冲突。

今天在openerp-开发群(69195329)中, 宁波-FEIGAMES(390717298) 介绍了通过python代码修改视图的例子 --重写view_get

def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False): 
if context is None:context = {} 
res = super(rhwl_gene, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar,submenu=False) 
if res['type']=="form": 
id = res['id'] 
//根据id去取得资料,并进行判断 
if 条件成立: 
doc = etree.XML(res['arch']) 
doc.xpath("//form")[0].set("edit","false") 
res['arch']=etree.tostring(doc) 
return res

原文地址:https://www.cnblogs.com/chjbbs/p/5305677.html