Ir al contenido
Menú
Se marcó esta pregunta
4 Respuestas
8962 Vistas

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

Avatar
Descartar
Mejor respuesta

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)

Avatar
Descartar
Mejor respuesta

Hai

     just add store = True . For yr code

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

Avatar
Descartar

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?

Mejor respuesta

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

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
0
oct 23
2792
4
dic 21
32295
2
dic 20
8815
4
ene 20
6033
2
ago 18
7213