【问题标题】:Add automatically a value of an existing field to a custom fields [Odoo14]自动将现有字段的值添加到自定义字段 [Odoo14]
【发布时间】:2021-02-04 09:20:04
【问题描述】:

我是 odoo 的新手,我想将来自 sale.order 的字段“client_order_ref”的值添加到来自 sale.order.line 的自定义字段“client_ref”中。

目标是在填写“client_order_ref”时自动填写此自定义字段。

型号:

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>

【问题讨论】:

  • 为什么使用One2many 字段来存储client_order_ref
  • 嗯我想我尝试了一些东西,但正如我所说,我是 odoo 的新手,所以如果你有任何建议,很高兴听到它:)

标签: python line odoo custom-fields customer


【解决方案1】:

订单行 (order_line) 字段已使用 order_id 字段(反名)将销售订单行链接到销售订单,因此您可以使用该字段从销售订单中获取字段值。

您可以将client_order_ref 定义为related 以提供当前记录(销售订单行)上的子字段(client_order_ref)的值:

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")  

【讨论】:

  • 哦,很好,我开始了解自定义字段的工作原理了,非常感谢它的工作原理!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 2019-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-08
  • 1970-01-01
相关资源
最近更新 更多