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

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

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

  1. def test(self):
  2. tomorrow = datetime.datetime.today() + datetime.timedelta(days=1)
  3. date_list = [(tomorrow + datetime.timedelta(days=x)).date() for x in range(30)]
  4. technician_line = self.env["allocated_technician_line"].search(
  5. [
  6. ("service_type_id", "=", self.service_type_id.id),
  7. ("time_slot", "=", self.time_slot),
  8. (
  9. "technician_id.user_id",
  10. "in",
  11. self.area_id.technicians.ids,
  12. ),
  13. (
  14. "allocated_technician_id.date",
  15. "in",
  16. date_list,
  17. ),
  18. ]
  19. )
  20. allocated_technician_schedule_ids = []
  21. for line in technician_line:
  22. allocated_sale_order = self.env["sale.order"].search(
  23. [
  24. (
  25. "preferred_service_schedule",
  26. "=",
  27. line.allocated_technician_id.id,
  28. ),
  29. ("service_type_id", "=", line.service_type_id.id),
  30. ("time_slot", "=", line.time_slot),
  31. ("state", "!=", "cancel"),
  32. ]
  33. )
  34. if self.env.context.get('active_id'):
  35. allocated_sale_order = allocated_sale_order.filtered(
  36. lambda r: r.id != self.env.context.get('active_id')
  37. )
  38. available_allocation = line.allocation - len(allocated_sale_order)
  39. if available_allocation > 0:
  40. allocated_technician_schedule_ids.append(line.allocated_technician_id.id)
  41. allocated_technician_schedule_recs = self.env["allocated_technician"].browse(allocated_technician_schedule_ids)
  42. user_ids = allocated_technician_schedule_recs.mapped('user_id').ids
  43. if (self.assigned_asc.id not in user_ids):
  44. 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

  1. # 之前的部分不变
  2. for line in technician_line:
  3. order_domain = [
  4. (
  5. "preferred_service_schedule",
  6. "=",
  7. line.allocated_technician_id.id,
  8. ),
  9. ("service_type_id", "=", line.service_type_id.id),
  10. ("time_slot", "=", line.time_slot),
  11. ("state", "!=", "cancel"),
  12. ]
  13. if self.env.context.get('active_id'):
  14. order_domain.append(('id', '!=', self.env.context.get('active_id')))
  15. order_count = self.env["sale.order"].search_count(order_domain)
  16. available_allocation = line.allocation - order_count
  17. # 以及后续的部分

希望这对你有帮助!

英文:

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:

  1. # no changes before
  2. for line in technician_line:
  3. order_domain = [
  4. (
  5. "preferred_service_schedule",
  6. "=",
  7. line.allocated_technician_id.id,
  8. ),
  9. ("service_type_id", "=", line.service_type_id.id),
  10. ("time_slot", "=", line.time_slot),
  11. ("state", "!=", "cancel"),
  12. ]
  13. if self.env.context.get('active_id'):
  14. order_domain.append(('id', '!=', self.env.context.get('active_id')))
  15. order_count = self.env["sale.order"].search_count(order_domain)
  16. available_allocation = line.allocation - order_count
  17. # 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:

确定