将列表传递到django模型对象

时间:2014-02-24 12:39:31

标签: python django models

我有一个这样的清单:

list = [['Amy Pond', 'R$30 of awesome for R$10', '10.0', '5', '456 Unreal Rd', "Tom's Awesome Shop\n"], ['Marty McFly', 'R$20 Sneakers for R$5', '5.0', '1', '123 Fake St Sneaker', 'Store Emporium\n'], ['Snake Plissken', 'R$20 Sneakers for R$5', '5.0', '4', '123 Fake St Sneaker', 'Store Emporium']]

我的模特是:

class Vendas(models.Model):
    purchasername=models.CharField(u'Nome do Comprador',max_length=100)
    itemdescription=models.CharField(u'Descrição do Item',max_length=100)
    itemprice=models.IntegerField(u'Preço do Item')
    purchasecount=models.IntegerField(u'Quantidade comprada')
    merchantaddress=models.CharField(u'Endereço do comprador',max_length=100)
    merchantname=models.CharField(u'Nome do Comprador',max_length=100)

我想将此列表按原样加载到Vendas表中。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

试试这个:

for i in list:
    a = Vendas(purchasername=i[0], itemdescription=i[1], itemprice=i[2], purchasecount=i[3], merchantaddress=i[4], merchantname=i[5])
    a.save()