如何将 one2many 下拉数据的值绑定到 odoo 12 中的列字段?

How to bind value from one2many dropdown data to column field in odoo 12?

我试图将名为 "price" 的字段从 one2many 下拉关系添加到父 table,这是我将第二个 table 与 many2one 关系联系起来?我可以这样做吗?如果是,该怎么做?

这是我的销售订单 class 代码:

# -*- coding: utf-8 -*-

from odoo import models, fields, api

class SalesorderSalesorder(models.Model):
    _name = 'salesorder.salesorder'

    no_faktur = fields.Char(String='No Faktur', required=True)
    kd_dealer = fields.Char(String='Kode Dealer', required=True)
    nm_dealer = fields.Char(String='Nama Dealer', required=True)
    tanggal_faktur = fields.Datetime(string='Tgl Faktur', default=fields.Datetime.now())
    keterangan = fields.Html(string='Keterangan')
    kd_sales = fields.Many2one('res.users', string='Kode Sales')
    details = fields.One2many('salesorderdetails','no_faktur','List Item')

这是我的销售订单详情:

# -*- coding: utf-8 -*-

from odoo import models, fields, api

class SalesOrderDetails(models.Model):
    _name = 'salesorderdetails'

    no_faktur = fields.Many2one('salesorder.salesorder')
    kd_produk = fields.Many2one('ms_produk.ms_produk','Kode Produk',required=True)
    nm_produk = fields.Char(String='Nama Produk', required=True)
    qty = fields.Integer(String='Qty', required=True)
    price= fields.Float(String='Price', required=True)
    subtotal = fields.Float('Subtotal (Rp)', readonly=True, compute='compute_subtotal', store=True)

    @api.one
    @api.depends('qty', 'price')
    def compute_subtotal(self):
        if self.qty and self.price:
            qty = self.qty
            price = self.price
            self.subtotal = qty * price

如何更新这一行

Price= fields.Float(String='Price', required=True)

还有一个名为 Price 的值,我已经在此 'ms_produk.ms_produk' table?

do you mean you want to update the price in salesorderdetails and refer to ms_produk.ms_produk? 如果有,可以查看官方文档

相关领域

A special case of computed fields are related (proxy) fields, which provide the value of a sub-field on the current record. They are defined by setting the related parameter and like regular computed fields they can be stored: use related attribute can attain this.

nickname = fields.Char(related='user_id.partner_id.name', store=True)

当您确认 kd_produk 中的项目时, 它将自动从中加载数据。

Price= fields.Float(String='Price', required=True, related='kd_produk.price')