2

I have these fields in my model:

seller = fields.Many2one('res.partner', string="Select Seller",domain="[('supplier','=',True)]")
products= fields.Many2one('product.template', string="Select Product" )

Now, i need to filter the second field as the user chooses a seller(first field) How do i set a domain onchange.

I am trying to do something like,

@api.onchange('seller')
    def onchange_field_seller(self):
        res = {}
        if self.seller:
            # return {'domain':{'product':[//what do i add here//]}}
        return res

I am using a many2many field to create the products in the seller creating form.

product_details = fields.Many2many('product.template',string="Products")

(Please note that the form with this field is different from the one in the question above). I am trying to get only those product entries that were created when i created the seller entry.Im really confused, how do i accomplish this?

p.ry
  • 409
  • 1
  • 8
  • 21
  • 1
    Could you please add the part of the product model with the many2many seller field? And try to stick to the Odoo Guideline of naming fields. For your two fields that would be `seller_id` and `product_id`, and if not already existing in product.template the sellers m2m field should be named `seller_ids`. – CZoellner Jun 19 '19 at 11:21
  • @CZoellner, please see the updated question. – p.ry Jun 19 '19 at 11:39
  • okay now i get it. – CZoellner Jun 19 '19 at 11:53

1 Answers1

5

In your case you can't use a "dynamic" domain but more of a pre-defined domain on product IDs.

@api.onchange('seller')
def onchange_field_seller(self):
    if self.seller:
        # filter products by seller
        product_ids = self.seller.product_details.ids
        return {'domain': {'product': [('id', 'in', product_ids)]}}
    else:
        # filter all products -> remove domain
        return {'domain': {'product': []}}
CZoellner
  • 13,553
  • 3
  • 25
  • 38