odoo show product in many2one from the same id

49 Views Asked by At

We know that in class sale_order has sale_order_line. Here is my py:

class sale_order(models.Model):
    _inherit = 'sale.order'
    order_ids = fields.One2many('sale.order.line','sales_order_id','Order Line')
    delivery_ids = fields.One2many('delivery.schedule','delivery_id','Delivery Line')

class sale_order_line(models.Model):
    _name = 'sale.order.line'
    sales_order_id = fields.Many2one('sale.order','Sales Order')
    product_id = fields.Many2one('product.product','Product')

class delivery_schedule(models.Model):
    _name = 'delivery.schedule'
    delivery_id = fields.Many2one('sale.order','Sales Order')
    delivery_product_id = fields.Many2one('sale.order.line','Product')

My question is, how to show product item in Many2one delivery_product_id only from product_id in that sale_order document?

1

There are 1 best solutions below

1
ariodwiponco On

Many2one to sale.order.line will point to line record instead of its product_id

the answer to your question is having another field related to its product_id field. Suggestion:

delivery_id = fields.Many2one('sale.order', string='Sale Order')
delivery_line_id = fields.Many2one('sale.order.line', string='Order Line')
delivery_product_id = fields.Many2one('product.product', related='delivery_line_id.product_id', string='Product')

Suggestion(s)

You could look up at how other module(s) declare models, for sale order and sale order line it does exist in sale module, so instead of using _name, you should go for _inherit.

And some fields have already been declared in the parent so you dont have to redeclare it again (like order_ids in so, sale_order_id in so line because it will be ambiguous with default order_id, and product_id will be redundant because the attributes already been there)