How to Import CSV Data into Django Models

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

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.

Django how to upload CSV file using Form to populate postgres database and display all items in browser

Thanks to this SO post I was able to find an answer by using a generator to decode the CSV line by line.

Here is the code:
views.py

def decode_utf8(line_iterator):
for line in line_iterator:
yield line.decode('utf-8')


def create_upload(request):
if request.method == 'GET':
form = UploadForm()
return render(request, 'upload.html', {'form': form})

form = UploadForm(request.POST, request.FILES)

# Validate the form
if form.is_valid():

# Get the correct type string instead of byte without reading full file into memory with a generator to decode line by line
products_file = csv.reader(decode_utf8(request.FILES['sent_file']))
next(products_file) # Skip header row

for counter, line in enumerate(products_file):
name = line[0]
sku = line[1]
description = line[2]

p = Product()
p.name = name
p.sku = sku
p.description = description
p.save()

messages.success(request, 'Saved successfully!')

return redirect('/show_product')


Related Topics



Leave a reply



Submit