英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论