当前位置:网站首页>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 .
边栏推荐
- IDEA 中使用 Hudi
- [point cloud processing paper crazy reading classic version 7] - dynamic edge conditioned filters in revolutionary neural networks on Graphs
- excel一小时不如JNPF表单3分钟,这样做报表,领导都得点赞!
- LeetCode 57. 插入区间
- 数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎
- Basic knowledge of network security
- LeetCode 513. Find the value in the lower left corner of the tree
- 推荐一个 yyds 的低代码开源项目
- What is the difference between sudo apt install and sudo apt -get install?
- Crawler career from scratch (IV): climb the bullet curtain of station B through API
猜你喜欢

LeetCode 75. Color classification

2022-2-14 learning the imitation Niuke project - send email

传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!
![[point cloud processing paper crazy reading classic version 8] - o-cnn: octree based revolutionary neural networks for 3D shape analysis](/img/fa/36d28b754a9f380bfd86d4562268c3.png)
[point cloud processing paper crazy reading classic version 8] - o-cnn: octree based revolutionary neural networks for 3D shape analysis

剑指 Offer II 029. 排序的循环链表

【点云处理之论文狂读经典版10】—— PointCNN: Convolution On X-Transformed Points

Crawler career from scratch (3): crawl the photos of my little sister ③ (the website has been disabled)

干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了

State compression DP acwing 291 Mondrian's dream

Education informatization has stepped into 2.0. How can jnpf help teachers reduce their burden and improve efficiency?
随机推荐
Redis learning (I)
LeetCode 241. Design priorities for operational expressions
Move anaconda, pycharm and jupyter notebook to mobile hard disk
[point cloud processing paper crazy reading classic version 11] - mining point cloud local structures by kernel correlation and graph pooling
excel一小时不如JNPF表单3分钟,这样做报表,领导都得点赞!
LeetCode 715. Range 模块
Save the drama shortage, programmers' favorite high-score American drama TOP10
LeetCode 515. Find the maximum value in each tree row
Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)
Sword finger offer II 091 Paint the house
Bert install no package metadata was found for the 'sacraments' distribution
LeetCode 30. 串联所有单词的子串
AcWing 785. Quick sort (template)
【Kotlin学习】类、对象和接口——定义类继承结构
低代码前景可期,JNPF灵活易用,用智能定义新型办公模式
推荐一个 yyds 的低代码开源项目
[advanced feature learning on point clouds using multi resolution features and learning]
[point cloud processing paper crazy reading classic version 14] - dynamic graph CNN for learning on point clouds
[kotlin learning] classes, objects and interfaces - define class inheritance structure
[graduation season | advanced technology Er] another graduation season, I change my career as soon as I graduate, from animal science to programmer. Programmers have something to say in 10 years