Django - How to import a csv file to mysql database using threading

This article explains how to import csv data in Django MySQL. To import data in MySQL we are going to use csv package. This package will help us to import our data in MySQL Database.

To import data in MySQL, create a html form to upload csv file, now we need to read the csv file, validate and process.

templates/import-csv.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha2/dist/css/bootstrap.min.css" rel="stylesheet" >
    <title>Import Csv</title>
</head>
<body>
     <div class="container">
        <div class="col-lg-6 form">
            <h1 class="text-primary">Import CSV</h1>
            <form action="{% url 'importcsv' %}" method="POST" class="border p-2 bg-white" enctype="multipart/form-data">
                {% csrf_token %}
                <div class="mb-3">
                    <label class="form-label">CSV</label>
                    <input type="file" name="csvfile" class="form-control" required>
                </div>
                <div class="mb-3 text-center">
                    <button type="submit" class="btn btn-outline-primary"><i class="fa fa-save"></i> Save</button>
                </div>
            </form>
    
        </div>
    </div>
   
</body>
</html>

After creating the HTML form we have to create route and one function to process the csv data.
import threading
import csv
from io import StringIO
from .models import Product

def importcsv(request):
   if request.method == "POST":
        csv_data = request.FILES["csvfile"]
        thread = threading.Thread(target=processcsv, args=(csv_data,))
        thread.start()
        messages.success(request,"CSV import started in background. Kindly referesh your grid page")
        return redirect("grid")
   return render(request,'import-csv.html')

This will be our route code, which will handle the form, after that we will create a function to process csv file data.
def processcsv(csv_data):
    csv_data = csv_data.read().decode('utf-8')
    csv_reader = csv.DictReader(StringIO(csv_data))
    headers = {'Content-Type': 'application/json'}
    for row in csv_reader:
        # print("row",row)
        data = {
            "name": row['name'],
            "brand": row['brand'],
            "description": row['description'],
            "gender": row['gender'],
            "num_images": row['num_images'],
            "price": row['price'],
            "primary_color": row['primary_color'],
            "sku": row["sku"]
        }
        Procduct(**data).save()
After all this we can run & check our product table, If you have followed everything same as given, It will be successfully imported. If you have any query let me know in comment. 

Happy coding...

Comments