Hello, I'm using Odoo 13.
I have a custom model, in my form view for that model I want to show, filter and update a related One2many field from product.pricelist. Show and update works, but the domain filter not.
My model:
class MyCustomModel(models.Model):
_name = 'mycustommodel'
product_id = fields.Many2one('product.product', 'Produkt', store=True)
pricelist_id = fields.Many2one('product.pricelist', 'Pricelist')
product_pricelist = fields.One2many('product.pricelist.item', 'pricelist_id', related='pricelist_id.item_ids', store=True)
@api.model
def default_get(self, fields):
res = super(Mcalculate, self).default_get(fields)
res.update({
'pricelist_id': self.env.ref('product.list0').id or False
})
return res
Now I want to dynamically change the domain of the product_pricelist field, that it only shows the pricelist items based on my product_id field.
I tried some onchange methods, but it shows only every product.pricelist.item, nothing gets filtered:
First try:
@api.onchange('product_id')
def onchange_product_id(self):
for rec in self:
if rec.product_id:
list_ids = self.env['product.pricelist.item'].search([('product_id', '=', rec.product_id.id)])
pricelist_ids_list = []
for data in list_ids:
pricelist_ids_list.append(data.id)
return {'domain': {'product_pricelist': [('id', 'in', pricelist_ids_list)]}}
Second try:
@api.onchange('product_id')
def onchange_product_id(self):
for rec in self:
if rec.product_id:
return {'domain': {'product_pricelist': [('product_id', '=', rec.product_id.id)]}}
Last try:
@api.onchange('product_id')
def onchange_product_id(self):
for rec in self:
if rec.product_id:
return {'domain': {'product_pricelist': [('product_tmpl_id', '=', rec.product_id.product_tmpl_id.id)]}}
I thought maybe it is not possible to filter related fields, but how can I show a One2many field for product.pricelist.item only with the selected product_id on my custom view in my custom model then?
Hi. Did you find a solution?