我有一段代码将SQLAlchemy对象处理成Python字典。在下面的代码中,agg
,containers
和sales
都是SQLAlchemy对象,其结构如下:
agg -- (one to many) --> containers -- (one to many) --> sales
当前,此循环的每次迭代大约需要0.1秒。反正有优化的方法吗?
_json = {}
_json['period_sales'] = []
for container in agg.containers:
s = time.time()
for sale in container.sales:
_json['period_sales'].append({
'start_date': None,
'end_date': None,
'sale': sale.purchases
})
print('{}'.format(time.time() - s))