[Fixed]-Relationship between two models Django/python

1👍

Two things you can try

  1. May be a minor improvement but don’t use a list for the cache, use a set instead. Lookup will be faster.

  2. Batch your save.

Try this:

from django.db import transaction


def import_times(request):
    transaction.set_autocommit(False)
    print "Import data from Times to OT"
    po_cache = set()
    for item in otreport.objects.all():

        if item.po_number in po_cache:
            continue

        times = Times.objects.filter(external_order=item.sales_order_shipset)
        records_count = 0
        for wi in times:
            records_count += 1
            po_cache.add(wi.external_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()
            if records_count >= 10000:
              transaction.commit()
              records_count = 0
    transaction.commit()
👤gipsy

Leave a comment