使用.save()将字典保存到数据库

时间:2016-04-05 02:38:13

标签: python django

现在我正在这样做:

for product in request.session['productList']:
        productData = Products_model(
                Fk_Cat_id=product['cat'],
                Fk_supplier_id=product['supplier'],
                Name=product['name'],
                Price=product['price'],
                )

        productData.save()

print 'All products has been saved'

但也许有类似的东西:

productData = Products_model.dictionary.(request.session['productList'])
productData.save()

谢谢!

1 个答案:

答案 0 :(得分:0)

如果您可以更改字典,以便键名与模型参数的名称匹配

products = [Products_model(**data) for data in request.session['productList']]
Products_model.objects.bulk_create(products)