Search code examples
pythonodoolinecustom-fields

Add automatically a value of an existing field to a custom fields [Odoo14]


I'm new to odoo and i want to add the value of the field "client_order_ref" from sale.order to my custom fields "client_ref" from sale.order.line.

The goal is to fill this custom fields automatically when "client_order_ref" is filled.

I want the client_order_ref fields value instead of "no records"

Models:

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

    sale_order_id = fields.Many2one('sale.order.line', string='Liaison Order Line')



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

    client_ref = fields.One2many('sale.order', 'sale_order_id', string="Référence Client", readOnly=True)

  

XML:

 <odoo>
  <data>
    <record model="ir.ui.view" id="view_quotation_form_inherited">
      <field name="name">view.quotation.form.inherited</field>
      <field name="model">sale.order</field>
      <field name="inherit_id" ref="sale.view_order_form" />
      <field name="arch" type="xml">

        <xpath expr="//field[@name='product_uom_qty']" position="before">
          <field name="client_ref">
              <tree>
                <field name="client_order_ref" String="Référence client"/>
              </tree>
            </field>
        </xpath>
        <xpath expr="//tree/field[@name='product_uom_qty']" position="before">
            <field name="client_ref">
              <tree>
                <field name="client_order_ref" String="Référence client"/>
              </tree>
            </field>
        </xpath>

      </field>
    </record>
  </data>
</odoo>

Solution

  • Order Lines (order_line) field uses already the order_id field (inverse name) to link sale order line to the sale order, so you can use that field to get field values from sale order.

    You can define client_order_ref as related to provide the value of a sub-field (client_order_ref) on the current record (sale orde line):

    class SaleOrder(models.Model):
        _inherit = 'sale.order'
    
        client_order_ref = fields.Char()
    
    
    class SaleOrderLine(models.Model):
        _inherit = 'sale.order.line'
    
        client_ref = fields.Char(related="order_id.client_order_ref")