[Django]-What's the easiest way to Import a CSV file into a Django Model?

2👍

I do it with a view using csv module. Here is an example of how I create bulk amount of users. This may not be the most efficient way to do it but it sure gets the job done.

import csv
from django.contrib.auth.hashers import make_password

def create_bulk_user(request):
    with open(os.path.join(settings.BASE_DIR, 'media', 'core', 'employees.csv')) as f:
        reader = csv.reader(f)
        for row in reader:
            user, created = User.objects.get_or_create(
                username=str(row[0]),

                defaults={
                    'password': make_password('ddff123#'),
                    'first_name': ' '.join(row[1].split()[:-1]),
                    'last_name': str(row[1].split()[-1])
                }
            )
            designation, created = Designation.objects.get_or_create(
                name=str(row[4]), defaults={}
            )
            department, created = Department.objects.get_or_create(
                name=str(row[3])
            )
            user.profile.designation = designation
            user.profile.department = department
            user.profile.proximity_id = str(row[2])
            user.profile.save()

    context = {}
    return render(request, "core/create_bulk_user.html", context)

Keep in mind that there is a bulk_create method for you to use but I haven’t used that over here.

11👍

A Django management command may be the best option for importing this data into your database. It gives you access to Django’s ORM whilst being separate from the request/response cycle. You can call it from the command-line with your .csv file as an argument.

Here’s a simple example.

import csv
from django.core.management import BaseCommand
from app.models import Question

class Command(BaseCommand):
    help = 'Load a questions csv file into the database'

    def add_arguments(self, parser):
        parser.add_argument('--path', type=str)

    def handle(self, *args, **kwargs):
        path = kwargs['path']
        with open(path, 'rt') as f:
            reader = csv.reader(f, dialect='excel')
            for row in reader:
                Question.objects.create(
                    attr1=row[0],
                    attr2=row[1],
                )
            

If you saved it as app/management/commands/load_questions.py you would invoke it as:

python manage.py load_questions --path /path/to/your/file.csv

1👍

A SQL client with an “import” button seems to be the obvious choice e.g., SQLiteStudio for SQLite or MySQL Workbench for MySQL.

However, if that doesn’t work, you can also do this in a single line using the pandas library:
pd.read_csv(filename).to_sql(tablename, con)

to_sql documentation

Leave a comment