콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
7 답글
22204 화면

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
2644
9
7월 21
62927
8
11월 19
7784
10
6월 16
33177
1
10월 15
9382