How to fix the problem with dynamic domains in Odoo?

I created a new model with the name product.service.type. Then in the model, product.productI also created a field Many2many(with a name service_typepointing to the model product.service.type).

Now I have a model testthat has fields product_idand service_type_id, both Many2onepoint to product.productand product.service.typerespectively.

When I select a product, I want to change the domain of the service type to display only the service types of the selected product. I managed it through onchange:

def onchange_product_id(self, cr, uid, ids, product_id, context=None):
    if product_id:
        product = self.pool.get('product.product').browse(
            cr, uid, [product_id], context=context)
        service_type_ids = product.service_type.mapped('id')
        return {
            'domain': {
                'service_type_id': [('id', 'in', service_type_ids)],
            },
        }

This works great, the problem is that you are editing the record (without creating a new one), because in this case onchangeit is not executed, and therefore the domain shows all types of services.

title. , , title, , Corp., Ltd. .., , , , , , .. . . title, is_company. , , ( ).

?

+4
2

, . .

  • product_id , xml.
  • ()
  • , , super()

:

@api.model
def search(self, args, offset=0, limit=None, order=None, count=False):
    context = self._context or {}

    # Display product list which has not included in CofA Template
    if context.get('product_service_id'):
        product = self.env['product.product'].browse(context.get('product_service_id'))
        service_type_ids = product.service_type.mapped('id')
        args += [('service_type_id', 'not in', service_type_ids)]

    return super(ProductServiceType, self).search(args,
                                               offset,
                                               limit,
                                               order,
                                               count=count)

XML:

<field name="product_id"/>
<field name="many2many_field" context="{'product_service_id': product_id}">

. API . API .

+1

test, 2 , , service_type product.product. service_type_id.

,

product.product :

  • service_type (m2m to product.service.type)

test :

  • product_id (m2o to product.product)
  • service_type_id (m2o to product.service.type)
  • available_service_type_ids (m2m, / service_type of product.product)
  • xml service_type_id [(id , in, available_service_type_ids and available_service_type_ids[0] and available_service_type_ids[0][2] or False)]
0

Source: https://habr.com/ru/post/1689056/


All Articles