1

I'm building an application with python and Django. I have several models but I need to get data from one model to another (the relation can be made with a SOSS (sales order number). I do have the logic to do that, but is not as efficient as I would wanted. It takes around 5-6 min to process the data.

My Model 1 has the relation number (po_number) and is related to Model 2 (there is called planning_number) It takes a lot of time because Model 2 has around 93,000 data lines.

This is my logic:

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()

My question is: Is there any way more efficient to reflect data from one model to another?

1
  • Hi, is your code meant to be run in routine or you run it manually una tantum? Commented Oct 23, 2019 at 9:08

1 Answer 1

1

Are your model1 and model2 related by a models.ForeignKey field (relating po_number and planning_order)? You can then use a QuerySet to get related objects.

What back end datastore are you using? Does it have indexes on the relevant fields? Have a look at setting the db_index=True option on the relevant fields in your model definitions (https://docs.djangoproject.com/en/1.8/ref/models/fields/#db-index).

Also see Improving performance of django DB query

Sign up to request clarification or add additional context in comments.

6 Comments

they are not related by a ForeignKey, Should I declare those as a unique key? and it will improve the relation?
Is there a 1:1 or 1:many relationship between Model1 and Model2?
Im using Mysql btw
Here you cna see withtimes = Model2.objects.filter(planning_order=item.po_number) that the planning number is the 1:1 relation between the two models, once is fould I get the data from Model 2 to Model 1.
You want me to share my models?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.