Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
3281 Lượt xem

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)] ```

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Thanks

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 6 25
15421
3
thg 4 25
5605
Compute Fields Đã xử lý
2
thg 7 24
3002
1
thg 1 24
1846
1
thg 7 22
2265