在Odoo中搜索字段的搜索方法优化

huangapple go评论59阅读模式
英文:

search Method Optimization for searching a field in odoo

问题

在这个方法中,需要优化以将 allocated_sale_order 搜索方法移到 for 循环之外。

英文:

actually I have a field in sale order which I update in onchange with a datetime field, and that has to be optimized, I am unable to figure out what to be done so need help, here is the method

here the time_slot field is a selection field with AM and PM

 def test(self):
        tomorrow = datetime.datetime.today() + datetime.timedelta(days=1)
        date_list = [(tomorrow + datetime.timedelta(days=x)).date() for x in range(30)]

        technician_line = self.env["allocated_technician_line"].search(
            [
                ("service_type_id", "=", self.service_type_id.id),
                ("time_slot", "=", self.time_slot),
                (
                    "technician_id.user_id",
                    "in",
                    self.area_id.technicians.ids,
                ),
                (
                    "allocated_technician_id.date",
                    "in",
                    date_list,
                ),

            ]
        )
        allocated_technician_schedule_ids = []
        for line in technician_line:
            allocated_sale_order = self.env["sale.order"].search(
                [
                    (
                        "preferred_service_schedule",
                        "=",
                        line.allocated_technician_id.id,
                    ),
                    ("service_type_id", "=", line.service_type_id.id),
                    ("time_slot", "=", line.time_slot),
                    ("state", "!=", "cancel"),
                ]
            )

            if self.env.context.get('active_id'):
                allocated_sale_order = allocated_sale_order.filtered(
                    lambda r: r.id != self.env.context.get('active_id')
                )

            available_allocation = line.allocation - len(allocated_sale_order)
            if available_allocation > 0:
                allocated_technician_schedule_ids.append(line.allocated_technician_id.id)

        allocated_technician_schedule_recs = self.env["allocated_technician"].browse(allocated_technician_schedule_ids)
        user_ids = allocated_technician_schedule_recs.mapped('user_id').ids

        if (self.assigned_asc.id not in user_ids):
            self["assigned_asc"] = False

need help in this method to be optimized just to bring allocated_sale_order search method out of for loop.

答案1

得分: 0

这只是一个小的优化,但你可以使用 search_count 代替 search。但你也必须重新安排如何使用 active_id

# 之前的部分不变
for line in technician_line:
    order_domain = [
        (
            "preferred_service_schedule",
            "=",
            line.allocated_technician_id.id,
        ),
        ("service_type_id", "=", line.service_type_id.id),
        ("time_slot", "=", line.time_slot),
        ("state", "!=", "cancel"),
    ]
    if self.env.context.get('active_id'):
        order_domain.append(('id', '!=', self.env.context.get('active_id')))
    order_count = self.env["sale.order"].search_count(order_domain)
    available_allocation = line.allocation - order_count
# 以及后续的部分

希望这对你有帮助!

英文:

That's only a small optimization, but you can use search_count instead of search. But you also have to rearrange how to use active_id for that:

# no changes before
for line in technician_line:
    order_domain = [
        (
            "preferred_service_schedule",
            "=",
            line.allocated_technician_id.id,
        ),
        ("service_type_id", "=", line.service_type_id.id),
        ("time_slot", "=", line.time_slot),
        ("state", "!=", "cancel"),
    ]
    if self.env.context.get('active_id'):
        order_domain.append(('id', '!=', self.env.context.get('active_id')))
    order_count = self.env["sale.order"].search_count(order_domain)
    available_allocation = line.allocation - order_count
# and so on

huangapple
  • 本文由 发表于 2023年6月5日 20:36:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76406487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定