user3786402
user3786402

Reputation: 99

How to store csv file to database in django?

In my project I want to enter 300 students every year. so it is not possible using add student form.So I want to create functionality for bulk data entry using csv or excel file. I have tried many things related to this but can't get solution.

** models.py **

class AddStudent(models.Model):
    enrollment_no = models.BigIntegerField(primary_key=True)
    student_name = models.CharField(max_length=500,null=True)
    gender = models.CharField(max_length=1,choices=GENDER_CHOICES)
    course = models.ForeignKey(CourseMaster, on_delete=models.DO_NOTHING, null=True)
    category= models.ForeignKey(CatMaster, on_delete=models.DO_NOTHING, null=True)
    admission_year = models.IntegerField(('year'), choices=YEAR_CHOICES, default=datetime.datetime.now().year)
    college = models.ForeignKey(CollegeMaster, on_delete=models.DO_NOTHING, null=True)
    branch = models.ForeignKey(BranchMaster,on_delete=models.DO_NOTHING, null=True)
    current_semester = models.IntegerField(null=True)
    address = models.CharField(max_length=1000,null=True)
    city = models.CharField(max_length=100,null=True)
    district = models.CharField(max_length=100,null=True)
    state = models.CharField(max_length=100,null=True)
    student_contact = models.BigIntegerField()
    parent_contact = models.BigIntegerField()

This is my models.py file and I want to store following fields through csv file. some fields are foreign key related to another models. so how to solve this?

** Views.py **

def upload_csv(request):
    data = {}
    if "GET" == request.method:
        return render(request, "add_student/bulk.html", data)
    # if not GET, then proceed
    try:
        csv_file = request.FILES["csv_file"]
        if not csv_file.name.endswith('.csv'):
            messages.error(request,'File is not CSV type')
            return HttpResponseRedirect(reverse("add_student:upload_csv"))
        #if file is too large, return
        if csv_file.multiple_chunks():
            messages.error(request,"Uploaded file is too big (%.2f MB)." % (csv_file.size/(1000*1000),))
            return HttpResponseRedirect(reverse("add_student:upload_csv"))

        file_data = csv_file.read().decode("utf-8")

        lines = file_data.split("\n")
        #loop over the lines and save them in db. If error , store as string and then display
        for line in lines:
            fields = line.split(",")
            data_dict = {}
            data_dict["enrollment_no"] = fields[0]
            data_dict["student_name"] = fields[1]
            data_dict["gender"] = fields[2]
            data_dict["course"] = fields[3]
            data_dict["category"] = fields[4]
            data_dict["admission_year"] = fields[5]
            data_dict["branch"] = fields[6]
            data_dict["current_semester"] = fields[7]
            data_dict["address"] = fields[8]
            data_dict["city"] = fields[9]
            data_dict["district"] = fields[10]
            data_dict["state"] = fields[11]
            data_dict["student_contact"] = fields[12]
            data_dict["parent_contact"] = fields[13]
            try:
                form = EventsForm(data_dict)
                if form.is_valid():
                    form.save()
                else:
                    logging.getLogger("error_logger").error(form.errors.as_json())
            except Exception as e:
                logging.getLogger("error_logger").error(repr(e))
                pass
    except Exception as e:
        logging.getLogger("error_logger").error("Unable to upload file. "+repr(e))
        messages.error(request,"Unable to upload file. "+repr(e))

    return HttpResponseRedirect(reverse("add_student:upload_csv"))

urls.py

path('upload/csv/', views.upload_csv, name='upload_csv'),

I have tried this example from internet but this is not working. Please suggest possible solutions. If some examples available on this please share. Please share some simple solution bcz I am new to django.

Upvotes: 1

Views: 4755

Answers (1)

Scratch'N'Purr
Scratch'N'Purr

Reputation: 10399

I had to do this for one of my projects. The way I did this was to create two models. One to define the student, and the other to define a file for import. Then, I had to create a post save hook if I used the option to bulk import a CSV file.

models.py

import os
import csv
from django.db import models
from django.dispatch import receiver
from django.db.models.signals import post_save
from django.conf import settings


class Student(models.Model):
    fname = models.CharField(max_length=32,
                             blank=False,
                             null=False)
    lname = models.CharField(max_length=32,
                             blank=False,
                             null=False)
    ... # additional model attributes


class StudentImportFile(models.Model):
    # upload to MEDIA_ROOT/temp
    student_import = models.FileField(upload_to="temp",
                                      blank=False,
                                      null=False)

    def save(self, *args, **kwargs):
        if self.pk:
            old_import = StudentImportFile.objects.get(pk=self.pk)

            if old_import.student_import:
                old_import.student_import.delete(save=False)

        return super(StudentImportFile, self).save(*args, **kwargs)


# post save signal
@receiver(post_save, sender=StudentImportFile, dispatch_uid="add_records_to_student_from_import_file")
def add_records_to_student_from_import_file(sender, instance, **kwargs):
    to_import = os.path.join(settings.MEDIA_ROOT, instance.student_import.name)

    with open(to_import) as f:
        reader = csv.DictReader(f)
        for row in reader:
            fname = row['First Name']
            lname = row['Last Name']
            ... # additional fields to read

            s = Student(fname=fname,
                        lname=lname,
                        ... # additional attributes
                       )
            s.save()

Upvotes: 1

Related Questions