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

Hey! guys. We found a solution to store readonlyonchange in DB through below code.. CREATE edit, it will store in DB

samp_cal1=fields.Integer(string="a")

samp_cal2=fields.Integer(string="b")

samp_cal3=fields.Integer(string="c",readonly=True)


@api.onchange('samp_cal1','samp_cal2')

def adding_samp(self):

       self.samp_cal3=self.samp_cal1+self.samp_cal2

@api.model

def create(self,vals):

    if self.samp_cal3:

        vals['samp_cal3']=self.samp_cal1+self.samp_cal2

    res = super(tokenreservation, self).create(vals)

    return res

@api.multi

def write(self,vals):

    if not self.samp_cal3:

        vals['samp_cal3'] = self.samp_cal1+self.samp_cal2

    res = super(tokenreservation, self).write(vals)

    return

아바타
취소
베스트 답변

Hai,

I create a field date of birth and age. Age field is specified a readonly true. Im able to store data in database .Im using create and edit method after age value is store in databse. Try to this way


staff_dob = fields.Date(string="Date of Birth")

staff_age = fields.Integer(string="Age",readonly=True,store=True)


def _get_cal(self,a):

if a:

    today = date.today()

    bdate = datetime.strptime(a, '%Y-%m-%d')

    rd = rdelta.relativedelta(today, bdate)

    val= rd.years

return val


Create a onchnage method:

@api.onchange('staff_dob')

def age_cal_fun(self):

    if self.staff_dob:

        a=self.staff_dob

        self.staff_age=self._get_cal(a)

    return {}



@api.multi

def write(self, vals):

    if vals.get('staff_dob', False):

        a=vals['staff_dob']

        vals['staff_age']=self._get_cal(a)

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



@api.model

def create(self, vals):

    if vals['staff_dob']:

        a=vals['staff_dob']

        vals['staff_age']=self._get_cal(a)

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

아바타
취소
베스트 답변

Hai

     just add store = True . For yr code

     samp_cal3=fields.Integer(string="c",readonly=True,store=True)

아바타
취소

i also have same issue , When i press save button the readonly with onchange field value was losed.

I tried this (store = True) in my field but nothing would changed.

If you have suggest any other way to solve this issue?

베스트 답변

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

아바타
취소
관련 게시물 답글 화면 활동
0
10월 23
3010
4
12월 21
33117
2
12월 20
9070
4
1월 20
6435
2
8월 18
7608