1

I have a function

    def test(self):
        tech_line = self.env['tech_line']
        allocated_technician = self.env['allocated_technician']
        users = self.env['res.users']
        tech_line = tech_line.search(
            [('service_type_id', '=', self.service_type_id.id)])
        al_a6 = self.env['tech_line'].filtered(lambda rec: rec.service_type_id.id == self.service_type_id.id)

        area = []
        area_new = []
        for tec in tech_line:
            territory = self.env['territory']
            territories = territory.search(
                [('technicians', 'in', tec.technician_allociated_id.user_id.id)])

            territories_lam = self.env['territory'].filtered(
                lambda t_lam: t_lam.technicians.id in tec.technician_allociated_id.user_id.id)

            for territory in territories:
                area.append(territory.id)
            for tet in territories_lam:
                area_new.append(tet.id)

        print('##################33', len(area))
        print('%%%%%%%%%%%%%%%%%%%%', len(area_new))
        
        print('$$$$$$$$$$$$$$$$$$$', tech_line)
        print('***************8***', al_a6)

this method when executed screen gets loading and I need to optimize this method, please do share your thoughts on how to optimize this code

I cannot limit the value which is generated from the search method as we need all of its value so instead of that I thought to use filtered instead of search method, but when I use filtered it gives an empty recordset. need help with that

Sidharth Panda
  • 404
  • 3
  • 17
  • 1
    `filtered` can get interesting when complex search domains lead to more than one db query, which can be less performant. Your searches look very simple, so try to use indexes first by setting `index=True` on field definitions for `service_type_id` and `technicians`. Would be helpful to know the definitions of those two fields, to give a better advice, so please add those definitions. – CZoellner Jun 02 '23 at 08:02
  • @CZoellner those two are many2one field and it is created on odoo studio, and in studio we cannot use index=True – Sidharth Panda Jun 02 '23 at 08:09
  • here what happens is in sale order, i have a field service_type_id and on changing the service_type_id this function is tregerred to fetch the area, so everytime it changes it searches arround 10,000 record and in for loop again 10,000 record so takes time, and i need to fasten its search, oviously i cannot use limit here – Sidharth Panda Jun 02 '23 at 08:13
  • 3
    To your question about `filtered`: you use it on empty odoo recordsets, which will give you empty recordsets. If you want to use `filtered` you have to use it for example on your search result (e.g. `tech_line` in line 5). – CZoellner Jun 02 '23 at 11:10

1 Answers1

2

You can avoid searching in the for loop by using a search on all users:

def test(self):
    tech_line = self.env['tech_line']
    tech_lines = tech_line.search(
        [('service_type_id', '=', self.service_type_id.id)])
    # get all users to avoid search in a for loop
    users = tech_lines.mapped("technician_allociated_id.user_id")
    # search territories
    territories = territory.search([('technicians', 'in', users.ids)])
    
    area = territories.ids
CZoellner
  • 13,553
  • 3
  • 25
  • 38
  • no actually its returing more value than before, anyway please let me know one thing – Sidharth Panda Jun 02 '23 at 10:16
  • like `sale order` and `sale order line` here we have `tech` and `tech line` so in my method up i am searching on `tech line`, and inside `tech line` i have multiple lines of`service_type_id` in a single `tech` model. please suggest the search method query to filter `service_type_id` where i can get only one id for `tech` which i search in `tech line` i hope this will solve my problem – Sidharth Panda Jun 02 '23 at 10:23
  • 1
    Maybe i have done a big mistake, but `area` should have been less, because in your code, you can have multiple list entries of the same id, whereas in my example recordsets are used. It's to difficult for me to follow your question without more information of models and there relations in form of code or a diagram. – CZoellner Jun 02 '23 at 11:09
  • @CZoellner There isn't any mistake, your code is doing the same thing than his. If there are mistake, there are probably in the original OP code. We can see a lot of filtered on empty recordSet. We can see a `(x, 'in', y.id)` and stuff. – Levizar Jun 03 '23 at 14:13
  • @Levizar there was an empty record set because in the `filter` the query is wrong, I was replacing the `search` method in `filter`, so I was requesting to optimize that – Sidharth Panda Jun 05 '23 at 02:23
  • @CZoellner its working as you suggested thanks. – Sidharth Panda Jun 05 '23 at 05:43
  • @CZoellner can you please optimize this question please https://stackoverflow.com/questions/76406487/search-method-optimization-for-searching-a-field-in-odoo – Sidharth Panda Jun 07 '23 at 12:22