将函数的输出存储在Django中的另一个函数中

时间:2019-06-12 07:11:25

标签: python django python-3.x django-models

我有一个Packer函数,其输出正试图保存在DispatchPlan中,如下所示:

for i in range(len(truck_objects)):

    DispatchPlan.objects.create(owner=request.user, comments="Autogenerated", truck_type=open_object,
                                truck_name=truck_objects[i], origin=k.split("-")[0],
                                destination=k.split("-")[1], total_trucks=1, material_type='Non-Fragile',
                                scheduled_date=item_date, offered_price=0, weight=item_weight_sum,
                                status='Hold', etd=open_etd_temp, route_link=route_map, eta=item_date,
                                route_distance=temp_distance, route_tat=temp, pk=new_quiz.id)

其中的truck_objects是

[<truck_name: Tempo 407 2500>, <truck_name: Tempo 407 2000>]

但是现在包装器还有另一个输出,即master_values,其中包含每辆卡车包装的物品。

master_values

[{<Truckdb: Truckdb object (3)>: [<ItemBatch: Chains & Chain Link Fence Fittings>, <ItemBatch: Chains & Chain Link Fence Fittings>]}, {<Truckdb: Truckdb object (2)>: [<ItemBatch:
Chains & Chain Link Fence Fittings>, <ItemBatch: Chains & Chain Link Fence Fittings>]}]

现在,如何将这个master_values保存在DispatchPlan中?

我可以想到的一种方法是嵌套循环,这样就可以通过在DispatchPlan模型中创建ArrayField将整个列表存储在卡车上。我还有其他方法可以存储这些数据吗?

Models.py

class DispatchPlan(models.Model):
    bid_status_choices = (
        ('Hold', 'Hold'), ('Active', 'Active'), ('Assigned', 'Assigned'), ('Dispatched', 'Dispatched'),
        ('Delayed', 'Delayed'), ('Delivered', 'Delivered'))
    mtypes = (('Fragile', 'Fragile'), ('Non-Fragile', 'Non-Fragile'))

    owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='dispatch_quizzes')
    comments = models.TextField(max_length=256)
    truck_type = models.ForeignKey(truck_type, on_delete=models.CASCADE, related_name='dispatch_type')
    truck_name = models.ForeignKey(truck_name, on_delete=models.CASCADE, related_name='dispatch_name')
    origin = models.CharField(max_length=255, default=0)
    destination = models.CharField(max_length=255, default=0)
    total_trucks = models.IntegerField(default=0)
    material_type = models.CharField(max_length=255, default=0, choices=mtypes)
    scheduled_date = models.DateTimeField(max_length=255, default=0)
    offered_price = models.IntegerField(default=0)
    weight = models.IntegerField(default=0)
    status = models.CharField(max_length=255, default='Active', choices=bid_status_choices)
    created_on = models.DateTimeField(auto_now_add=True)
    route_distance = models.IntegerField(default=0)
    route_tat = models.CharField(max_length=255, default=0)
    etd = models.DateTimeField(default=timezone.now)
    eta = models.DateTimeField(default=timezone.now)
    route_link = models.CharField(max_length=255, default=0)

0 个答案:

没有答案