I have a backend grid with a product_id field that needs to be filtered according to the value of another field that is the id of a seller. So, when you open the popup for the product search, I want taht the domain is changed dynamically (all products if seller is empty).
0
1 Risposta
0
Migliore risposta
Add a hidden variable in the grid (product_supplier_ids), which represents our domain. This variable will change dynamically every time I select a different vendor in mask.
class AccountMoveLine(models.Model):
_inherit = 'account.move.line'
product_supplier_ids = fields.Many2many('product.product', compute='_compute_product_supplier_ids')
@api.depends('move_id.partner_id')
def _compute_product_supplier_ids(self):
for account_move_line_id in self:
ids_of_product_suppliers = []
if bool(account_move_line_id.move_id.partner_id.supplier_to_product_ids):
for supplier_product_id in account_move_line_id.move_id.partner_id.supplier_to_product_ids:
ids_of_product_suppliers.append(supplier_product_id.product_id.id)
else:
ids_of_product_suppliers = self.env['product.product'].search([]).ids
account_move_line_id.product_supplier_ids = [(6, 0, ids_of_product_suppliers)]
<record id="view_move_form_supplier_to_product" model="ir.ui.view"> <field name="name">account.move</field> <field name="model">account.move</field> <field name="inherit_id" ref="account.view_move_form" /> <field name="arch" type="xml"> <xpath expr="//page[@id='invoice_tab']/field[@name='invoice_line_ids']//tree/field[@name='product_id']" position="replace"> <field name="product_supplier_ids" /> <field name="product_id" domain="[('id', 'in', product_supplier_ids)]" /> </xpath> </field> </record>