Vivi
Vivi

Reputation: 41

How to automatically populate a field according to another field in a different model in Odoo13?

I am building a model named "Wrap"(similar to product packagings) which allows users to create a wrap for each selected product from the sale order line tree.

from odoo import models, fields, api, _


class Wrap(models.Model):
    _name = 'sale.wrap'
    _description = 'wraps in SO tree view'

    name = fields.Char(string='Name', required=True)
    product = fields.Many2one('product.product', string='Product')
    quantity = fields.Integer()

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

    wrap = fields.Many2one('sale.wrap', string='Wrap')

I had added the field named wraps to the sale order line tree which directs users to the wrap create form.

wrap create form with product field which has to be updated

Now I want the product field in this model to get automatically updated according to the corresponding product in that sale order line, how is this done.

Selected product in sale order line tree

I am learning this framework myself and this is the only help I can get, please help, Thanks in advance.

Upvotes: 0

Views: 263

Answers (2)

Huzaifa  Elnaeem
Huzaifa Elnaeem

Reputation: 11

try to use onchange method

warp = fields.Many2one('sale.order.line','Warp')

like :

@api.onchange('product')
def onchange_product(self):
   res = []
   for rec in self :
     vals = {'name':rec.value1,'field2':rec.line.value2,etc}
     res.append(vals)

Upvotes: 0

Kenly
Kenly

Reputation: 26688

You can pass default values in the context:

<field name="wrap" context="{'default_field_name': field_value}"

Upvotes: 1

Related Questions