Skip to Content
Menu
This question has been flagged
1 Odpoveď
623 Zobrazenia

class PurchaseOrder(models.Model):

    _inherit = 'purchase.order'

   

    po_type = fields.Selection([

        ('normal_order', 'Normal Purchase Quation/Order'),

        ('service_order', 'Service Order')

        ],default='normal_order',string="Purchase Order Type")



class PurchaseOrderLine(models.Model):

    _inherit = 'purchase.order.line'

   

   

    product_id = fields.Many2one('product.product', string='Product', domain=[('purchase_ok', '=', True)], change_default=True, index='btree_not_null')

   

In Odoo 17, use the onchange function to modify the domain of the product field so that only 'service' type products are displayed when the po_type is set to 'service_order'

can we achive this without declaring addtional field ?? 

Avatar
Zrušiť
Best Answer

Hi,

Please refer to the code:

from odoo import api, fields, models


class PurchaseOrderLine(models.Model):

    _inherit = "purchase.order.line"


    @api.onchange("order_id")

    def _onchange_order_id_set_domain(self):

        """Dynamically restrict product domain based on order type."""

        if self.order_id and self.order_id.po_type == "service_order":

            return {

                "domain": {

                    "product_id": [

                        ("purchase_ok", "=", True),

                        ("detailed_type", "=", "service"),

                    ]

                }

            }

        else:

            return {

                "domain": {

                    "product_id": [("purchase_ok", "=", True)]

                }

            }


Hope it helps.

Avatar
Zrušiť