我正在用python和Django构建一个应用程序。我有几个模型,但是我需要从一个模型到另一个模型获取数据(可以用一个SOSS(销售订单号)建立关系)。我确实有这样做的逻辑,但没有我想要的那样高效。处理数据大约需要5-6分钟。在
我的模型1有关系号(po峎u数),与Model 2相关(有称为planning_number),这需要很多时间,因为模型2有大约93000条数据线。在
这是我的逻辑:
def import_withtimes(request):
print "importando With Times a ots report"
po_cache = list()
for item in Model1.objects.all():
if item.po_number in po_cache:
continue
withtimes = Model2.objects.filter(planning_order=item.po_number)
for wi in withtimes:
po_cache.append(wi.planning_order)
item.wms = wi.wms_order
item.status = wi.shipment_status
item.aging = wi.status_date
item.carrier = wi.service_level
item.add_date = wi.order_add_date
item.asn_validation = wi.asn_sent_time
item.docs_add_date = wi.docs_received_time
item.save()
我的问题是:有没有更有效的方法将数据从一个模型反映到另一个模型?在
您的})?然后可以使用QuerySet获取相关对象。在
model1
和model2
是否由models.ForeignKey
字段关联(关联po_number
和{您使用什么后端数据存储?它是否有相关字段的索引?看看在模型定义(https://docs.djangoproject.com/en/1.8/ref/models/fields/#db-index)中的相关字段上设置
db_index=True
选项。在另请参见Improving performance of django DB query
相关问题 更多 >
编程相关推荐