当前位置:网站首页>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 .
边栏推荐
- LeetCode 535. TinyURL 的加密与解密
- AcWing 787. Merge sort (template)
- Crawler career from scratch (V): detailed explanation of re regular expression
- AcWing 785. 快速排序(模板)
- 2022-2-13 learn the imitation Niuke project - Project debugging skills
- [point cloud processing paper crazy reading frontier version 10] - mvtn: multi view transformation network for 3D shape recognition
- On a un nom en commun, maître XX.
- C language programming specification
- Computing level network notes
- Crawler career from scratch (3): crawl the photos of my little sister ③ (the website has been disabled)
猜你喜欢

LeetCode 715. Range 模块
![[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks](/img/61/aa8d0067868ce9e28cadf5369cd65e.png)
[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks

excel一小时不如JNPF表单3分钟,这样做报表,领导都得点赞!

推荐一个 yyds 的低代码开源项目

教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?

【Kotlin学习】类、对象和接口——定义类继承结构

Build a solo blog from scratch

Vscode编辑器右键没有Open In Default Browser选项

Data mining 2021-4-27 class notes

AcWing 788. 逆序对的数量
随机推荐
[kotlin learning] classes, objects and interfaces - define class inheritance structure
常见渗透测试靶场
[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu
Computing level network notes
传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!
Crawler career from scratch (IV): climb the bullet curtain of station B through API
网络安全必会的基础知识
图像修复方法研究综述----论文笔记
2022-2-14 learning xiangniuke project - Session Management
2022-2-13 learning xiangniuke project - version control
Digital statistics DP acwing 338 Counting problem
LeetCode 513. 找树左下角的值
Introduction to the basic application and skills of QT
即时通讯IM,是时代进步的逆流?看看JNPF怎么说
[set theory] order relation (chain | anti chain | chain and anti chain example | chain and anti chain theorem | chain and anti chain inference | good order relation)
Move anaconda, pycharm and jupyter notebook to mobile hard disk
LeetCode 324. Swing sort II
推荐一个 yyds 的低代码开源项目
Using variables in sed command
AcWing 785. Quick sort (template)