1
votes

I added 2 fields in 'sale.order.line' object. Let's say 'field1' and 'field2', those are readonly field. The value of the 2 fields will appear whenever the product is change in order line.

When I select a product, it shows the value of the two fields but when save it, the value will back 0, not stored.

Here's my code:

class sale_order_line(models.Model):
    _inherit = 'sale.order.line'

    field1 = fields.Float('One')
    field2 = fields.Float('Two')

    @api.multi
    def product_id_change(self, pricelist, product, qty=0,
            uom=False, qty_uos=0, uos=False, name='', partner_id=False,
            lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False):

        res = super(sale_order_line, self).product_id_change(pricelist, product, qty,
                uom, qty_uos, uos, name, partner_id,
                lang, update_tax, date_order, packaging, fiscal_position, flag)

        if product:
            one = self.search([('product_id', '=', product), ('partner_id', '=', partner_id)])
            two = self.search([('product_id', '=', product), ('partner_id', '!=', partner_id)])
            if customer:
                field1 = one[-1]
                res['value']['field1'] = field1
            if other:
                field2 = two[-1].
                res['value']['field2'] = field2

        return res
2

2 Answers

0
votes

In Odoo framework we are now allowing to set readonly fields value, as well as readonly fields will not be getting in vals in create and write methods.

So if you set those readonly fields value in onchange methods then also it will not persist it's value because by nature it's readonly, it will not gives you any errors.

Purpose : The aims behind to define readonly attributes is to behave same through the all states of the record on UI and user can not change it's value and mainly define for display purpose.That is why readonly fields are not accessible for edit in onchange method.

Solution:

You need to override CREATE or WRITE method in following manner.

@api.model
def create(self, vals):
    ###### 
    # WRITE YOUR LOGIC HERE AND BRING THOSE VALUE INTO VARIABLE AND THEN UPDATE IT IN VALS
    VARIABLE_1 = SEARCH YOUR DATA
    VARIABLE_2 = SEARCH YOUR DATA
    vals.update({field1' : VARIABLE_1, 'field_2' : VARIABLE_2})
    return super(sale_order_line, self).create(vals)

Update vals (record dictionary) by setting those readonly fields in to dictionary before calling super method or update those fields after calling super method.

There is an alternative solution for your problem. In Odoo Apps one module available from that system will store readonly value in the database.

Read Only ByPass

0
votes

It is because readonly mode works only for display. In this case value from fields will not send to server-side.

You can override method create of sale.order.line. It should be something like this:

class sale_order_line(models.Model):
    _inherit = 'sale.order.line'

    @api.model
    def create(self, vals):
        # just example
        vals[u'field1'] = 2.03
        vals[u'field2'] = 3.05

        return super(sale_order_line, self).create(vals)

Hope this helps you.