我正在尝试将Django对象保存在表中,但是它会自动转换为字符串
我要保存的对象
w = [ < ItemBatch: Survey >]
models.py
class DispatchPlan(models.Model):
items = ArrayField(models.CharField(max_length=300), default=default_thing)
def default_thing():
return ['No Items Packed']
truck_name = models.ForeignKey(truck_name, on_delete=models.CASCADE, related_name='dispatch_name')
我做了什么
DispatchPlan.objects.create(items=w)
但是当我在表中检查相同内容时,便会这样保存
{Survey}
我想将其保存为对象本身,该怎么做?
编辑:
Views.py
for i in v:
c = {}
for m, n in i.items():
truck_objects.append(get_object_or_404(truck_name, id=m))
m = get_object_or_404(Truckdb, id=m)
c[m] = [get_object_or_404(ItemBatch, id=v_id) for v_id in n]
packed_items.append(c[m])
for i in c[m]:
item_weight_sum += i.weight
item_date = i.date
truck_filled = (i.weight*len(c[m])/m.Weight)*100
master_values.append(c)
for i in range(len(truck_objects)):
for j in range(len(master_values)):
if i == j:
for q, w in master_values[j].items():
DispatchPlan.objects.create(truck_name=truck_objects[i], items=w)
其中
v = [{136: [2, 2, 2]}, {254: [2, 2, 2]}]
和
master_values:
[{<Truckdb: Truckdb object (136)>: [<ItemBatch: Iphone>, <ItemBatch: Iphone>, <ItemBatch: Iphone>]}, {<Truckdb: Truckdb object (254)>: [<ItemBatch: Iphone>, <ItemBat
ch: Iphone>, <ItemBatch: Iphone>]}]
卡车对象:
[<truck_name: Tempo 407 1500>, <truck_name: Tempo 407 1500>]