How To Read CSV File and Save The Content To The Database In Django Rest

Data is the new oil like many will say, which is becoming increasing true owing to the fact that organizations both small and big have come to realize the important of data and what they can do with it. These data can be retrieve from many sources and in different format.
Having worked with multiple organizations that are data driven, I’ve come to realize that alot of organizations want to have their data synced in a central place, where it can be easily accessed.
When the medium for this central place happens to be a database, it often comes down to a developer to find a way of syncing all these various data.

In this article I will be showing you how to retrieve data from a csv file and store it’s values in a database using Django.
Let’s get started…

I will assume you have knowledge of building API’s with django and django-rest because we won’t be covering that here.

To get started run the following command, it will install django, django-rest and pandas which we will be using to read our csv file

$ pip install django djangorestframework pandas

In any of your django applications inside your project, add the following code snippets to the models.py

from django.db import modelsclass File(models.Model):
id = models.CharField(primary_key=True, max_length=6)
staff_name = models.CharField(max_length=100)
position = models.CharField(max_length=200)
age = models.IntegerField()
year_joined = models.CharField(max_length=4)
def __str__(self):
return self.staff_name

The “File” table is where we will be storing our values from the csv file.
Next create a serializers.py in the same application and add the following

from rest_framework import serializers
# remember to import the File model
class FileUploadSerializer(serializers.Serializer):
file = serializers.FileField()
class SaveFileSerializer(serializers.Serializer):

class Meta:
model = File
fields = "__all__"

FileUploadSerializer is what will allow user’s upload the csv file. Next add the following in views.py file of the same application

from django.shortcuts import render
from rest_framework import generics
import io, csv, pandas as pd
from rest_framework.response import Response
# remember to import the File model
# remember to import the FileUploadSerializer and SaveFileSerializer
class UploadFileView(generics.CreateAPIView):
serializer_class = FileUploadSerializer

def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
file = serializer.validated_data['file']
reader = pd.read_csv(file)
for index, row in reader.iterrows():
new_file = File(
id = row['id'],
staff_name= row["Staff Name"],
position= row['Designated Position'],
age= row["Age"],
year_joined= row["Year Joined"]
)
new_file.save()
return Response({"status": "success"},
status.HTTP_201_CREATED)

Below is a sample of how the data in the csv file is arranged.

id  Staff Name      Designated Position   Age   Year Joined   
1 Baron Chibuikem Python Web 30 2019
2 Priye Andy Python Data 40 2019
3 Ugo C IT support 23 2020
4 Pelumi Osande Devops 14 2020
5 Jessica olande Project Manager 56 2020

In your urls.py, add a path for the upload

from django.urls import path
from files.views import UploadFileView
urlpatterns = [
path('upload/', UploadFileView.as_view(), name='upload-file')
]

You can register the File model in your admin so you are able to inspect in your django admin interface. To do that add the following code snippet

from django.contrib import admin
from files.models import File, FileType
class FileAdmin(admin.ModelAdmin):
list_display = ["id", "staff_name", "position", "age", "year_joined"]
admin.site.register(File, FileAdmin)

With all that done, you can go ahead and start your django server, navigate to the url you added which may look like this “http://127.0.0.1:8000/upload/”.
Django Rest will display a page for you to upload your file, go ahead and upload a csv file using the sample data above and check your database to confirm it works.

I hope you found this short article helpful. Thanks for reading to the end.

A fullstack software developer constantly building shit tone of things with python and javascript and occasionally do some writing.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store