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?
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 ).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.