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

i created a on_change function, that changes the value of a certain field float. it does change its value but when i save it, the values reverts to default. in my case 0. 

i tried removing the readonly, and it works. But the thing is that i need it to be a readonly so it can't be edited by the user.

頭像
捨棄
最佳答案

Simple...
set the force_save to 1 to allow on_change events to be able to save on the server side.

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

頭像
捨棄
最佳答案

Onchange method doesn't pass the values on server side, it remains on client side until you save the record.

But this doesn't work on readonly fields. For that you have to write a tricky code.

You have to override create and write methods in which you can call your onchange method that will return a dictionary from which you can fetch the value of your readonly field and pass it in dictionary of create or write method.

So user will be able to see onchange effect on readonly field and the value will be saved on database with your tricky code.

Sample:

def write(cr, uid, ids, vals, context=None):
    for rec in self.browse(cr, uid, ids, context=context):
        #Pass required value of fields in onchange
        res = self.your_onchange(cr, uid, [rec.id], rec.field1, rec.field2)
        if res.has_key('values'):
            vals.udate({'your_readonly_fields_value': res.get('your_readonly_fields_value')})
    return super(your_class_name, self).write(cr, uid, ids, vals, context=context)

def create(cr, uid, vals, context=None):
    #Pass required value of fields in onchange
    res = self.your_onchange(cr, uid, [], vals.get('field1'), vals.get('field2'))
    if res.has_key('values'):
        vals.udate({'your_readonly_fields_value': res.get('your_readonly_fields_value')})
    return super(your_class_name, self).create(cr, uid, vals, context=context)

頭像
捨棄
作者

Thanks, I will try :) thanks a lot

Because my onchange function is pretty elaborated, I didn't want to execute it again when saving/creating data, so I decided to use the self.write at the end of the onchange function. Something like this: #-------------------------------------------------- your_onchange(cr, uid, ids, field1, field2, context=None): context = context or {} vals = {} .... .... # All my elaborated stuff to get my_new_value... .... vals.update ( {'your_readonly_fields_value': my_new_value } ) self.write( cr, uid, ids, vals, context ) return {'value': vals} #----------------------------------------------------

最佳答案
The Sudhir Arya (SA) worked for me, but my onchange function is pretty elaborated, so I didn't want to execute it again when saving/creating data, so I decided to use a self.write at the end of the onchange function. Something like this:

#--------------------------------------------------

your_onchange(cr, uid, ids, field1, field2, context=None):
   context = context or {}
   vals = {}
   ....
   .... # All my elaborated stuff to get my_new_value...
   ....
   vals.update ( {'your_readonly_fields_value': my_new_value } )
   self.write( cr, uid, ids, vals, context )
   return {'value': vals}
#----------------------------------------------------

頭像
捨棄
最佳答案

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

頭像
捨棄
最佳答案

my solution:

change readonly to invisible:

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

add related field:

    product_to_show = fields.Many2one(
        'product.product',
        readonly=True,
        related='product_id'
    )


show related:

<field name="product_to_show"/>

頭像
捨棄
相關帖文 回覆 瀏覽次數 活動
1
10月 24
1581
9
7月 21
62802
8
11月 19
7678
10
6月 16
33059
1
10月 15
9262