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

I have an age calculation function (onchange) and it calculates and shows up in the form when I enter the data. But the data is not saved in the data base. Please help with the code.


    @api.onchange('dob')

     def onchange_age_calculate(self):

      if not self.dob:

       self.age = 0

     else:

      today_date = datetime.datetime.now().year

      date_split = self.dob.split('-',1)

      birth_year = date_split[0]

      self.age = today_date -int(birth_year)


    _columns = {

                'name': fields.char(string="Name"), 

                'dob' : fields.date('Date of Birth'),

                'age' : fields.float( string='Age')


I have imported date time and the required library files hope so

아바타
취소
베스트 답변

That could be happening if you declare readonly the field age in the view. The readonly fields are not saved into the database even when they change their value through an onchange. To solve just remove the readonly attr or recalculate it in the create/write method of your model or define it as an computed field. There are solutions to send the readonly fields to the create/write methods but it's not out of the box

아바타
취소
베스트 답변

hello try this


   @api.onchange('dob')

   @api.depends('dob')

     def onchange_age_calculate(self):

      if not self.dob:

       self.age = 0

     else:

      today_date = datetime.datetime.now().year

      date_split = self.dob.split('-',1)

      birth_year = date_split[0]

      self.age = today_date -int(birth_year)

 

      name=  fields.Char(string="Name"), 

      dob' = fields.Date('Date of Birth'),

      age= fields.Float( string='Age')



then replace your class like this

class your_class_name(models.Model):


Thanks and I hoped i help you.

아바타
취소
베스트 답변

Anand,

As axel said you cant store readonly values in DB, i got some workout around it to do that..

To store readonly values also in future, u can have a little modification at your js part:

at wed/static/src/js/views/view_form.js        under FormView class, at method '_process_save()'

approx at line# 873, under "else if (f.name !== 'id' && (!self.datarecord.id || f._dirty_flag))"

replace the code with below one....

else if (f.name !== 'id' && (!self.datarecord.id || f._dirty_flag)){

//commenting below lines to allow sotring readonly values also

// if (!f.get("readonly")) {

               // values[f.name] = f.get_value(true);

    // } else {

            // readonly_values[f.name] = f.get_value(true);

    // }

    values[f.name] = f.get_value();

     if (f.get("readonly"))

            readonly_values[f.name]= f.get_value();

}

Hope this will help you

아바타
취소
베스트 답변

# Specify below header file in your code:

from datetime import datetime

from dateutil import relativedelta as rdelta

from datetime import date

#create a two field in your py file

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

staff_age = fields.Integer(string="Age")

# We can use onchange method in my staff_dob field .

@api.onchange('staff_dob')

def age_cal_fun(self):

if self.staff_dob:

today = date.today()

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

rd = rdelta.relativedelta(today, bdate)

self.staff_age = rd.years

#Above this code is help for you

아바타
취소
관련 게시물 답글 화면 활동
On_change: help! 해결 완료
2
8월 15
4193
0
4월 24
1883
4
11월 23
6006
0
10월 23
1758
0
12월 22
2647