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
Looking for Recommendation on How to Convert PDF into Structured Format
In Python Can One Implement Mixin Behavior Without Using Inheritance
Ruby Equivalent to Python's Help()
Is There a Function That Checks If a Character in a String Is a Letter in the Alphabet? (Swift)
Function Which Returns the Least-Squares Solution to a Linear Matrix Equation
Extract Column Value Based on Another Column Pandas Dataframe
How to Add Title to Subplots in Matplotlib
Create Dynamic Urls in Flask with Url_For()
How to Import CSV Data into Django Models
Dead Simple Example of Using Multiprocessing Queue, Pool and Locking
How to Convert a Dictionary into a List of Tuples
Get Protocol + Host Name from Url
Cosine Similarity Between 2 Number Lists
Call Python Function from Matlab
How to Take Partial Screenshot with Selenium Webdriver in Python
How to Avoid Http Error 429 (Too Many Requests) Python
Why Is Variable1 += Variable2 Much Faster Than Variable1 = Variable1 + Variable2