从一个模型获取数据到另一个Django/Python

2024-06-16 08:37:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用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()

我的问题是:有没有更有效的方法将数据从一个模型反映到另一个模型?在


Tags: in模型addnumbercachedate关系status
1条回答
网友
1楼 · 发布于 2024-06-16 08:37:18

您的model1model2是否由models.ForeignKey字段关联(关联po_number和{})?然后可以使用QuerySet获取相关对象。在

您使用什么后端数据存储?它是否有相关字段的索引?看看在模型定义(https://docs.djangoproject.com/en/1.8/ref/models/fields/#db-index)中的相关字段上设置db_index=True选项。在

另请参见Improving performance of django DB query

相关问题 更多 >