跳至內容
選單
此問題已被標幟
7 回覆
22426 瀏覽次數

when i save the record the onchange function work to my readonly if it is in editable its working correctly how can i do it using it  as an not editable field.Is there any modules to install to that purpose.I am using openerp 7

頭像
捨棄
最佳答案

Hi,

In the odoo versions from v11, you can use the force_save attribute along with the read-only field.


Example:

<field name="qty_to_consume" readonly="1" force_save="1"/>


In odoo10 and below, you can use this custom module from OCA: https://apps.openerp.com/apps/modules/10.0/web_readonly_bypass/


Usage


This module changes the behaviour of Odoo by propagating on_change modifications to readonly fields to the backend create and write methods.


To change that behavior you have to set context on ir.actions.act_window:

<record id="sale.action_quotations" model="ir.actions.act_window">
    <field name="context">{'readonly_by_pass': True}</field>
</record>

or by telling fields allowed to change:

<record id="sale.action_quotations" model="ir.actions.act_window">
    <field name="context">
        {'readonly_by_pass': ['readonly_field_1', 'readonly_field_2',]}
    </field>
</record>

On one2many fields, you can also pass the context in the field definition:

<field name="one2many_field" context="{'readonly_by_pass': True}"/>



Thanks

頭像
捨棄
最佳答案

Yes,

you can change the value of a readonly field using onchange finction like this,

fname = fields.Char('First Name')
name = fields.Char(readonly=True, store=True, string='Name')
    
@api.onchange('fname')
def _onchange_name(self):
     if self.fname:
         self.name = str(self.fname)
@api.model
def create(self, vals):
     if vals['fname']:
         vals['name'] = vals['fname']
     res = super(Demo, self).create(vals)
     return res
@api.multi
def write(self, vals):
     if vals['name']:
         vals['name'] = vals['fname']
     res = super(Demo, self).write(vals)
     return res



頭像
捨棄
最佳答案

Hello

I hope below code will help you

In my case have Total year field is readonly and based on 'Date of birth' Total year will be update

Using onchange method, Can get Total year on field but when save that record Total Year field to set blank

Solution:-

Create new dummy field of total year and set that dummy field value on original field

Example:-

Python file

total_year = fields.Float()

total_year_copy = fields.Float()

from datetime import date​​

# Onchange method

@api.onchange('dob')
def onchange_dob(self):
today = date.today()
self.total_year = self.total_year_copy = today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day))

# Create method

@api.model

def create(self, vals):

if 'total_year_copy' in vals:

vals.update({'total_year': vals.get('total_year_copy')})

return super(Project, self).create(vals)

# Write method

@api.multi

def write(self, vals):

if 'total_year_copy' in vals:

vals.update({'total_year': vals.get('total_year_copy')})

return super(Project, self).write(vals)

Xml File

<field name="total_year" readonly="1"/>

<field name="total_year_copy" invisible="1"/>

Hope this help you to save readonly records

Best Regards,

Ankit H Gandhi

頭像
捨棄
最佳答案

Hi,

You may check this module:

https://apps.openerp.com/apps/modules/8.0/web_readonly_bypass/

頭像
捨棄
作者

I have installed this module and checked but my onchange function not properly working

最佳答案

Kindly refer to https://bharatrdevnani.wordpress.com/2016/02/04/readonly-attributes-onchange-odoo


Best Regards,

Bharat

頭像
捨棄