Django orm 實現批量插入資料

2022-05-01 23:51:10 字數 662 閱讀 9787

在hibenate中,通過批量提交sql操作,部分地實現了資料庫的批量操作。但在django的orm中的批量操作卻要完美得多,真是乙個驚喜。

資料模型定義

首先,定義乙個例項使用的django資料庫模型product,只是象徵性地定義了兩個欄位name和price。

from django.db import

models

class

product(models.model):

name = models.charfield(max_length=200)

price = models.decimalfield(max_digits=10, decimal_places=2)

批量插入資料

批量插入資料時,只需先生成個一要傳入的product資料的列表,然後呼叫bulk_create方法一次性將列表中的資料插入資料庫。

product_list_to_insert =list()

for x in range(10):

product name

' + str(x), price=x))

product.objects.bulk_create(product_list_to_insert)

**:

Django ORM 中的批量操作

from django.db import models class product models.model name models.charfield max length 200 price models.decimalfield max digits 10,decimal places 2 ...

oracle實現批量插入

最近專案上需要批量插入資料到oracle當中,這裡總結三個方法 一.帶自增id的形式,注意這邊是沒有values insert into test id,name,age,address select seq test id.nextval id,a.from select from dual a二...

mybaits實現批量插入 script指令碼

mybatis中實現批量插入資料,我們可以使用指令碼進行開發,還需要用到foreach標籤 service層 dao層 insert options usegeneratedkeys true keyproperty id void batchinsert list list 其中foreach中c...