跳至内容
菜单
此问题已终结
4 回复
13299 查看

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

形象
丢弃
相关帖文 回复 查看 活动
2
8月 15
4018
0
4月 24
1737
4
11月 23
5753
0
10月 23
1495
0
12月 22
2433