Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

2

(solution) How to store field readonly in database with onchange method

By
Annadurai
on 7/8/16, 3:59 AM 599 views

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

2
kirubanidhi
On 7/8/16, 4:03 AM

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)

2
Saravanakumar P
On 7/9/16, 6:46 AM

Hai

     just add store = True . For yr code

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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 7/8/16, 3:59 AM
Seen: 599 times
Last updated: 7/27/16, 6:40 AM