Pointer
Pointer

Reputation: 2186

Save value from readonly field in odoo

I have 3 fields on form field sum is readonly. After click on Save data from readonly field is not store in database.

Example:

class myClass(models.Model):

number_1 = fields.Integer(store=True,default=0)
number_2 = fields.Integer(store=True,default=0)
sum = fields.Integer(store=True)

@api.onchange('number_1','number_2')
def compute_sum(self):
    total = self.number_1 + self.number_2
    self.sum = total

Upvotes: 3

Views: 4519

Answers (3)

user3623213
user3623213

Reputation: 91

from openerp import models, fields, api,exceptions

class your_model(models.Model):
    _inherit = 'your.model'

    field_1 =fields.Many2one('model.a', required="True", string="Field 1")
    field_2 = fields.Integer(string = 'Field 2')

    # onchange of field_1 we are populating value in field_2

    @api.onchange('field_1')
    def _onchange_field_1(self):
        if field_1:
            # your logic goes here to get value for field_2
            self.field_2 = some_value

    # As we know that when we get the value on onchange method, 
    # the desired field convert into readonly mode and its value does not save into the database
    # to save the value into the database we are going to override create and write method and in that method we explicitly save the value

    @api.model
    def create(self,vals):
        if self.field_2:
            vals['field_2']=self.field_1
        res = super(your_model, self).create(vals)
        return res

    @api.multi
    def write(self,vals):
        if not self.field_2:
            vals['field_2'] = self.field_1
        res = super(your_model, self).write(vals)
        return res

Reference: Save readonly field value in database

Upvotes: 0

Er.Ankit H Gandhi
Er.Ankit H Gandhi

Reputation: 656

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

Upvotes: 0

CZoellner
CZoellner

Reputation: 14778

I would prefer a computed field in this situation. Readonly fields won't be saved by formular changes (for example on change events). In Odoo 11 there was introduced a new option force_save for this behaviour in views, but previous versions don't have this option (except with community modules e. g. web_readonly_bypass by OCA).

The solution for computed fields:

class myClass(models.Model):

    number_1 = fields.Integer()
    number_2 = fields.Integer()
    sum = fields.Integer(compute="compute_sum", store=True)

    @api.depends('number_1','number_2')
    @api.multi
    def compute_sum(self):
        for record in self:
            total = record.number_1 + record.number_2
            record.sum = total

There is no need for view definition changes. And there also is no need for store parameter in normal Integer fields. Default value 0 is already the default on Integer so ne need to define it.

You don't have to explictly define readonly in the view for sum because a computed field without inverse method is readonly by default.

Upvotes: 3

Related Questions