问题描述:
当一个字段设为readonly =True 后,在form表单,即使你用onchange方法改变了值
但也不能保存到数据库当时。平时在这样的要求,form表单有些字段要展示给用户,但又要达到不
能更改,当通过其它字段变化,那个字段也会变化,这时那个字段发生了变化,当然要存到数据库当
中
分析解决:
这个form.js 一个小bug ,修正一下即可
view_form.js 853行
- if (!f.get("readonly")) {
- values[f.name] = f.get_value();
- } else {
- readonly_values[f.name] = f.get_value();
- }
+ values[f.name] = f.get_value();
+ if(f.get("readonly"))
+ readonly_values[f.name] = f.get_value();
前面有 “-” 号的,就要注释掉
前面有 “+”号的,加入来即可
===============================
上面的方法,副作用很大,第二种方法相对是更好的选择
用下面这个小模块来解决
web_readonly_bypass
链接:https://pan.baidu.com/s/1smyFBnZ 密码:9zxf
只要先安装这个模块
在自己对应的action 加入要保存的字段, 如下:
vip_grade_name = fields.Char(string='Customer Vip Grade',readonly=True)
@api.multi
def onchange_partner_id(self, partner_id):
res = super(SaleOrder, self).onchange_partner_id(partner_id)
if partner_id:
customer_orders = self.env['sale.order'].search([('partner_id','=',partner_id),('state','not in',('cancel','draft','sent'))],limit=1)
if not customer_orders :
res['value']['is_new'] = True
else:
res['value']['is_new'] = False
partner = self.env['res.partner'].browse(partner_id)
res['value']['vip_grade_name'] = partner.vip_grade_id.name
res['value']['customer_ref'] = partner.ref
else:
res['value']['is_new'] = False
res['value']['customer_ref'] = None
res['value']['vip_grade_name'] = None
res['value']['vip_potential'] = False
return res
=====================
<record id="action_unified_order" model="ir.actions.act_window">
<field name="name">All Orders</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">sale.order</field>
<field name="view_type">form</field>
<field name="view_id" ref="sale.view_order_tree"/>
<field name="view_mode">tree,form,calendar,graph</field>
<field name="search_view_id" ref="sale.view_sales_order_filter"/>
<field name="context">{'readonly_by_pass': ['vip_grade_name']}</field>
<field name="help" type="html">
<p class="oe_view_nocontent_create">
Click to create a quotation, the first step of a new sale.
</p><p>
Odoo will help you handle efficiently the complete sale flow:
from the quotation to the sales order, the
delivery, the invoicing and the payment collection.
</p><p>
The social feature helps you organize discussions on each sales
order, and allow your customers to keep track of the evolution
of the sales order.
</p>
</field>
</record>
就这样,reaonly 的字段,就可以根据其它字段变化,而更改保存到数据库当中, 若不用这个模块,系统是不保存到数据库,虽然表面变了。