当前位置:网站首页>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 .
边栏推荐
- 【Kotlin学习】类、对象和接口——定义类继承结构
- LeetCode 241. Design priorities for operational expressions
- LeetCode 508. 出现次数最多的子树元素和
- The "booster" of traditional office mode, Building OA office system, was so simple!
- [point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion
- Install third-party libraries such as Jieba under Anaconda pytorch
- 【点云处理之论文狂读前沿版9】—Advanced Feature Learning on Point Clouds using Multi-resolution Features and Learni
- IDEA 中使用 Hudi
- Sword finger offer II 091 Paint the house
- AcWing 785. 快速排序(模板)
猜你喜欢
Education informatization has stepped into 2.0. How can jnpf help teachers reduce their burden and improve efficiency?
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
LeetCode 513. 找树左下角的值
LeetCode 1089. Duplicate zero
Low code momentum, this information management system development artifact, you deserve it!
图像修复方法研究综述----论文笔记
【点云处理之论文狂读经典版8】—— O-CNN: Octree-based Convolutional Neural Networks for 3D Shape Analysis
我們有個共同的名字,XX工
【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?
Common penetration test range
随机推荐
常见渗透测试靶场
Shell script kills the process according to the port number
[advanced feature learning on point clouds using multi resolution features and learning]
The difference between if -n and -z in shell
CSDN markdown editor help document
Education informatization has stepped into 2.0. How can jnpf help teachers reduce their burden and improve efficiency?
LeetCode 1089. Duplicate zero
npm install安装依赖包报错解决方法
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
Beego learning - JWT realizes user login and registration
What is an excellent fast development framework like?
Beego learning - Tencent cloud upload pictures
LeetCode 508. The most frequent subtree elements and
Principles of computer composition - cache, connection mapping, learning experience
【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定
Go language - Reflection
AcWing 785. 快速排序(模板)
Redis learning (I)
浅谈企业信息化建设
Save the drama shortage, programmers' favorite high-score American drama TOP10