1 定义model
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=200)
price = models.DecimalField(max_digits=10, decimal_places=2)
2 批量插入 key word: bulk_create
product_list_to_insert = list()
for x in range(10):
product_list_to_insert.append(Product(name='product name ' + str(x), price=x))
Product.objects.bulk_create(product_list_to_insert)
3 批量更新数据
Product.objects.filter(name__contains='name').update(name='new name')
4 批量删除数据
Product.objects.filter(name__contains='name query').delete()
所谓“批量操作”中,对每一个实体的更新操作,都会生成一条update语句,然后只是把好几个update语句一次性提交给数据库服务器而已
参考:http://www.cnblogs.com/alphajx/p/5097114.html