Member-only story

How to upload CSV using Django(Bulk_Create)

Ramesh Ponnusamy
3 min readApr 30, 2020

When we develop a Web Application, handling data is vital as there will be a lot of forms/submissions from the user.

Maintaining and processing data is a critical task for any Backend team (like me :))

We should follow a set of protocols to upload data into the database. I learnt this mantra when I built my first ever application named Dossier (To read more: dossier)

I always use python scripts to import CSV data into databases as my ETL Script. and this helps in all kinds of data importing.

When it comes to Django web development, we should follow the procedure to build our code clean and adopt a framework that makes the life of the engineer easier!

One way of practice to import data is :

  1. Follow the object-oriented approach and ensure every data should go into the database only through the model and not by using raw SQL queries.
  2. You can either follow Class-Based-Views or Functions-Based-Views according to your operation.

Handle request CSV file as follows:

paramFile = io.TextIOWrapper(request.FILES['employeefile'].file)

Read the POST request file and convert it into DICT:

portfolio1 = csv.DictReader(paramFile)
list_of_dict = list(portfolio1)

Note: Here I'm not using Pandas, because sometimes it's not handling INT data type columns which have null values. It…

--

--

Ramesh Ponnusamy
Ramesh Ponnusamy

Written by Ramesh Ponnusamy

Data-Architect, SQL Master,Python ,Django, Flask dev, AI prompting, Linked-in: https://www.linkedin.com/in/ramesh-ponnusamy/ mail : ramramesh1374@gmail.com

Responses (2)