Skip to Content
Menu
This question has been flagged
2 Replies
5241 Views

Hi there,

i try to set a domain in an on_change function on project_task. Selecting the sale order item a sub-product has to be selected and i want to set the domain on this many2one field.

class bundleProduct(models.Model):

    _name = "bundle.product"


    name = fields.Many2one('product.product',string="Name")

    prod_id = fields.Many2one('product.product', string="Product Id")

    quantity = fields.Integer(string="Quantity", default="0")

    unit_id = fields.Many2one('product.uom', 'Unit of Measure ')


class bundleProductTask(models.Model):

    _inherit = "project.task"


    #Sub - Product

    bundle_product_id = fields.Many2one('bundle.product')


    @api.onchange('sale_line_id')

    def onchange_sale_line_id(self):

        bundle_product_ids = self.env['bundle.product'].search([('prod_id', '=', self.sale_line_id.product_id.id)])

        return {'domain': {'bundle_product_id': [('id', 'in', bundle_product_ids.id)]}}

Creating a task - selecting a sale order item - no sub-product for selection will be shown. I have tried to set the domain inside the xml view without any success.

Thanks for your help, 
Kind regards, Franz

Avatar
Discard
Best Answer

return {'domain': {'bundle_product_id': [('id', 'in', bundle_product_ids.ids)]}}

Avatar
Discard
Author Best Answer

@Ibrahim 

You make my day !! Thank you so much, it works perfectly !!!

Franz

Avatar
Discard

Welcome Franz,

Remember, if you do not add limit=1 to your search method, it can return multiple records. Probably in your case, it does return 1 record because you only have 1 that satisfies your condition but if u have many records you would get an error.

Then you get the records' ids which return a list of ids.