1
votes

I have a model with one2many field mes_indicator_sub_group_ids.I tried this but nothing is filtered even that I know there is a record with id 5:

  http.request.env['table.test'].sudo().search([('id', '=', int(category))]).filtered(lambda x: x.mes_indicator_sub_group_ids.ids in [5]) 

what I need is to filter record of table.test that have only one record in mes_indicator_sub_group_ids and its ID == 5.

EDITS

I have a model model.mes_indicator_group, which contains two one2many fields, mes_indicator_sub_group_ids and mes_indicator_ids. I want to get recordset which relates to model.mes_indicator_group with id=1 and mes_indicator_sub_group_ids with id=5. For example I want to get mes_indicator_ids from first group and fifth subgroup. Just get data, not unlink or write. Sorry for bad english.

1
can you put your code, what do you mean by get and what will you do with themCharif DZ

1 Answers

1
votes

You want to filter record that have only one record in the one2many field and it's Id should be 5, just make sure the length is one and the id of record is 5:

http.request.env['table.test'].sudo().search(your_domain).filtered(lambda x: len(x.mes_indicator_sub_group_ids) == 1 and x.mes_indicator_sub_group_ids.id  == 5)

don't worry about x.mes_indicator_sub_group_ids.id it will not cause a problem because it contains only one record.

The problem here you are using search([('id', '=', int(category))]) with this type of domain you will always have only one record, it will be nicer if you write an if statement that explain what you are doing for your team, and when you deal with ID use browse it's more clear:

   rec = http.request.env['table.test'].sudo().browse(int(category))
   if len(rec.mes_indicator_sub_group_ids) == 1 and rec.mes_indicator_sub_group_ids.id  == 5):
         # do what you need to do with this record

EDITS

Here you have two solution if you want to delete them completly from database or you just want to remove them from one2many it's up to you.

    rec = http.request.env['table.test'].sudo().browse(int(category))
    # this remove all record from database keeps only record with 5
    rec.mes_indicator_sub_group_ids.filtered(lambda x : x.id != 5).unlink()  

    # this will remove the record from the one2many but keeps them in teh database
    rec.mes_indicator_sub_group_ids = [(6,0, [5])]