使用python在数据库中插入多个列表



我对如何使用循环插入多个list bycolumn感到困惑,让我说输出我想在数据库中是这样的

name         percentage       is_fixed_amount
PWD           20                   0
Senior        20                   0
OPD            6                   0
Corporators   20                   0
Special                            1

What I've try但它插入了多个数据,并没有反映数据库中的实际数据,有人知道解决方案吗?感谢您的回复。

discount = ['PWD','Senior Citizen','OPD','Corporators','Special']
discount_percentage = ['20','20','6','20','']
fixed_amount = [False,False,False,False,True] 

for dis in discount:
for per in discount_percentage:
for fix in fixed_amount:
discount_val = Discounts(name=dis,percentage = per,is_fixed_amount =fix)
discount_val.save()

您正在使用嵌套循环将值插入数据库,该数据库通过每个项目3列表discount,discount_percentage,fixed_amount的组合进行迭代。您应该尝试使用1 for循环进行迭代,并通过索引

访问列表中的每个项。
discount = ['PWD','Senior Citizen','OPD','Corporators','Special']
discount_percentage = ['20','20','6','20','']
fixed_amount = [False,False,False,False,True] 

for i in range(len(discount)):
discount_val = Discounts(name=discount[i],percentage = discount_percentage[i],is_fixed_amount =fixed_amount[i])
discount_val.save()

最新更新