pythonodooodoo-12one2many

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


i trying to add field named "price" from one2many dropdown relationship to the parent table, which is i relate the second table with many2one relationship? can i do this? if yes, how to do this?

here is my salesorder class code :

# -*- 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')

and here is my salesorderdetails :

# -*- 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

how do i update this line

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

with a value named Price too that i already have in this 'ms_produk.ms_produk' table?


Solution

  • do you mean you want to update the price in salesorderdetails and refer to ms_produk.ms_produk? if so, you can check out official document.

    Related fields

    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)
    

    while you confirm the item in kd_produk, it will auto load data form it.

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