当前位置:网站首页>Django operates Excel files through openpyxl to import data into the database in batches.
Django operates Excel files through openpyxl to import data into the database in batches.
2022-07-03 09:20:00 【weixin_ forty-six million three hundred and seventy-one thousan】
1、 In some scenarios, administrators often need to import data in batches , First write the data to excel In the table , Upload to the system , The final will be excel The data in is imported into the database of the system in batches .
1.1、test.xlsx File data format

The first line's name : It must be the field name corresponding to the database , So that we can use ** Break up the dictionary , Achieve rapid creation .
2、 How to implement the view class
class TestExcelView(ViewSet):
authentication_classes = []
permission_classes = []
throttle_classes = []
@action(methods=['get'],detail=False)
def testexcel(self,request):
path=os.path.join(settings.MEDIA_ROOT,'students_excels','test.xlsx')
wb = openpyxl.load_workbook(path)
ws = wb.active
max_row = ws.max_row
max_column = ws.max_column
all_lis=[]
lis=[]
for row in range(1,max_row+1):
dic={}
for column in range(1,max_column+1) :
if row==1:
# Add the first line to the list
lis.append(ws.cell(row,column).value)
continue
# Take the line header as key, The remaining rows correspond to columns as value, Get dict, One in a row dict
dic.setdefault(lis[column-1],ws.cell(row,column).value)
if row>1:
# First line dict It's empty , There is no need to join
all_lis.append(dic)
try:
for user_dic in all_lis:
models.User.objects.create_user(**user_dic)
return ApiResponse(msg=' User created successfully ',data=all_lis)
except:
return ApiResponse(data={'erroe':'excel There is something wrong with the document. , First check the file format '})3、all_list Data format

A row is a database record , Directly when creating users **dic, You can create records .
The principle of other batch generated data is the same .
边栏推荐
- Education informatization has stepped into 2.0. How can jnpf help teachers reduce their burden and improve efficiency?
- We have a common name, XX Gong
- 【点云处理之论文狂读经典版11】—— Mining Point Cloud Local Structures by Kernel Correlation and Graph Pooling
- Liteide is easy to use
- Pic16f648a-e/ss PIC16 8-bit microcontroller, 7KB (4kx14)
- 一个优秀速开发框架是什么样的?
- 2022-2-13 learning the imitation Niuke project - home page of the development community
- AcWing 786. Number k
- 精彩回顾|I/O Extended 2022 活动干货分享
- Digital statistics DP acwing 338 Counting problem
猜你喜欢

低代码起势,这款信息管理系统开发神器,你值得拥有!

即时通讯IM,是时代进步的逆流?看看JNPF怎么说
![[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis](/img/c6/5f723d9021cf684dcfb662ed3acaec.png)
[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis

CSDN markdown editor help document

LeetCode 241. Design priorities for operational expressions

网络安全必会的基础知识

【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?

Recommend a low code open source project of yyds

【点云处理之论文狂读经典版7】—— Dynamic Edge-Conditioned Filters in Convolutional Neural Networks on Graphs

剑指 Offer II 091. 粉刷房子
随机推荐
Principles of computer composition - cache, connection mapping, learning experience
We have a common name, XX Gong
String splicing method in shell
Crawler career from scratch (IV): climb the bullet curtain of station B through API
excel一小时不如JNPF表单3分钟,这样做报表,领导都得点赞!
【点云处理之论文狂读前沿版12】—— Adaptive Graph Convolution for Point Cloud Analysis
LeetCode 438. Find all letter ectopic words in the string
【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定
LeetCode 508. 出现次数最多的子树元素和
STM32F103 can learning record
数字化转型中,企业设备管理会出现什么问题?JNPF或将是“最优解”
【点云处理之论文狂读经典版14】—— Dynamic Graph CNN for Learning on Point Clouds
AcWing 785. 快速排序(模板)
The difference between if -n and -z in shell
一个优秀速开发框架是什么样的?
Use the interface colmap interface of openmvs to generate the pose file required by openmvs mvs
Severity code description the project file line prohibits the display of status error c2440 "initialization": unable to convert from "const char [31]" to "char *"
【点云处理之论文狂读经典版12】—— FoldingNet: Point Cloud Auto-encoder via Deep Grid Deformation
【Kotlin学习】类、对象和接口——定义类继承结构
LeetCode 871. Minimum refueling times