【问题标题】:Relationship between two models Django/pythonDjango/python 两个模型之间的关系
【发布时间】:2016-12-19 16:28:30
【问题描述】:

我有两个模型,我需要从一个表(模型)获取数据到另一个,我的逻辑有效,但需要一段时间(大约 5 分钟),这是因为 Times Table 有大约 90,000 条数据行。我将首先发布我的两个模型,然后发布我用来关联和确定我从 Times 表中得到的内容的逻辑:

模型 1:

class otreport(models.Model):
    sales_order_shipset = models.CharField(max_length=30)
    wms = models.CharField(max_length=50)
    status = models.CharField(max_length=200)
    aging = models.DateField(null=True, blank=True)
    carrier = models.DateField(null=True, blank=True)
    add_date = models.DateTimeField(null=True, blank=True)
    asn_validation = models.DateTimeField(null=True, blank=True)
    docs_add_date = models.DateTimeField(null=True, blank=True)
    po_number = models.CharField(max_length=20, unique=True)

模型 2:

class Times(models.Model):
    external_order = models.CharField(max_length=20)
    planning_order = models.CharField(max_length=20)
    wms_order = models.CharField(max_length=40)
    customer_id = models.CharField(max_length=50)
    service_level = models.CharField(max_length=100)
    state = models.CharField(max_length=5)
    status_date = models.DateTimeField(null=True, blank=True)
    order_date = models.DateTimeField(null=True, blank=True)
    order_add_date = models.DateTimeField(null=True, blank=True) 
    asn_sent_time = models.DateTimeField(null=True, blank=True)
    docs_received_time = models.DateTimeField(null=True, blank=True) 
    docs_processing_time = models.CharField(max_length=100) 

这是我的逻辑,它发生了什么(为了更快地做到这一点,我使用了一个列表来不重新读取销售 po_numbers)并将外部订单与销售订单相关联以获得我想要的信息:

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

        if item.po_number in po_cache:
            continue

        times = Times.objects.filter(external_order=item.sales_order_shipset)
        for wi in times:
            po_cache.append(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()

我的问题是,是否有人有更好的想法来更有效地运行它,如果您需要更多数据或详细信息,请随时询问:),

感谢您的宝贵时间

【问题讨论】:

    标签: python django list for-loop models


    【解决方案1】:

    你可以尝试两件事

    1. 可能是一个小的改进,但不要为缓存使用列表,而是使用集合。查找会更快。

    2. 批量保存。

    试试这个:

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

    【讨论】:

    • 所以你建议使用 for po_cache = set() ?
    • 是的。和 po_cache.add 添加到它
    • po_cache.add 而不是 po_cache.append ?让我试一试。对于成功提交我没有得到它,我只得到 import commit, on_commit, savepoint_commit, set_autocommit... 我应该使用哪一个?
    • 让我试一试,让你知道我的结果:) 与我的结果相比
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 2017-06-26
    相关资源
    最近更新 更多