Django - Upload PDF and Excel file to mysql database

1.6k Views Asked by At

I have written a code to upload an excel sheet and save the contents to mysql database. Below is the code to upload the file in memory.

form = UploadFileForm(request.POST,request.FILES)
if form.is_valid():
    file_in_memory = request.FILES['file'].read()
    wb = load_workbook(filename=BytesIO(file_in_memory), data_only=True)

Now, I want to upload a PDF file and save the path to mysql database using the same function. For instance, If excel has record A then there is an associated PDF file which I have to link. How do I handle this situation? Using formset is a solution? I am aware of multi-file upload but I assume that will save the file path to the database directly.

1

There are 1 best solutions below

3
On

I believe you can do this directly in a model.py using FileField.

from django.db import models
class UploadFile(models.Model):
    file_in_memory=models.FileField(upload_to='uploads/')