Skip to Content
Menu
This question has been flagged

Hello, 

I'm trying to display a view with certain products that have multiple BoM's. I've created a computed field that labels which records should be displayed. I'm trying to create a search function so that the records in interest can be displayed as a filter but am having trouble creating the function. Currently trying to append record.id's of interest into a list and returning the list within the search domain but that is not working. Any help would be much appreciated. Please see code below and thanks in advance! 


I tried the following code but it returns an empty data list. I think there's something wrong with how I'm getting the id of the current record and appending it to the list that is returned. 


``` class products_ppa_bom_check(models.Model):

 _inherit = ['product.template'] 

ppa_multi_bom = fields.Selection([ ('true', 'True'), ('false', 'False'), ('na', 'Not Applicable')], string="PPA Multi BOM Check", compute='_compute_ppa_multi_bom', search='_search_ppa_multi_bom') 

def _compute_ppa_multi_bom(self): 

    for record in self: 

        count = record.env['mrp.bom'].search_count(['|', ('product_tmpl_id', '=', record.id), ('byproduct_ids.product_id.product_tmpl_id', '=', record.id)]) 

        if (count > 1) and ('PPA' in str(record.default_code)): 

            record.ppa_multi_bom = 'true' 

        elif (count == 1) and ('PPA' in str(record.default_code)):                 

             record.ppa_multi_bom = 'false' 

        else: record.ppa_multi_bom = 'na' 

    def _search_ppa_multi_bom(self, operator, value): 

        ids = [] 

        for record in self: 

            count = record.env['mrp.bom'].search_count(['|', ('product_tmpl_id', '=', record.id), ('byproduct_ids.product_id.product_tmpl_id', '=', record.id)]) 

            if (count > 1) and ('PPA' in str(record.default_code)): 

                ids = ids.append(record.id) 

        return[('id', 'in', ids)] ```

Avatar
Discard
Best Answer

Hi,
You can have a look at this video explaining the same:  How To Define Search Function For Field In Odoo

Thanks

Avatar
Discard
Best Answer

Hi,


Try to use ids.append(record.id) without assigning it back to ids.


def _search_ppa_multi_bom(self, operator, value):

    ids = []

    for record in self:

        count = record.env['mrp.bom'].search_count(['|', ('product_tmpl_id', '=', record.id), ('byproduct_ids.product_id.product_tmpl_id', '=', record.id)])

        if (count > 1) and ('PPA' in str(record.default_code)):

            ids.append(record.id)

    return [('id', 'in', ids)]


Hope it helps

Avatar
Discard
Related Posts Replies Views Activity
1
Jun 25
15389
3
Apr 25
5563
2
Jul 24
2626
1
Jan 24
1835
1
Jul 22
2245