Hello!
In my app, via post form I receive a PDF file.
models.py
class UploadFiles(models.Model):
pdf = models.FileField(upload_to='pdf/')
In my views I’ve been able to convert the PDF file to images using the pdf2image library and storing the converted images in any folder I want. Now, I want to make the conversion in models so that I can store the path of the converted image in database and the images in “media/pdf/” (for further loop calls in templates).
But I’m confused about how to do this. What I’ve tried so far has been this:
#views.py
def converter(request):
if request.method == 'POST':
form = UploadFilesForm(request.POST ,request.FILES )
if form.is_valid():
pdf = form.save()
pdf.convert()
else:
form = UploadFilesForm()
context = {'form':form}
return render(request, 'converter.html',context)
I’m having trouble specifically in the models.py section.
models.py
class UploadFiles(models.Model):
pdf = models.FileField(upload_to='pdf/')
def convert(self):
# Convert pdf to an image ( filename = converted image)
UploadFiles.create(filename,'JPEG')
self.save()
How should I save the converted images in my models? Is this the correct way to do it or should I save the converted images in other model somehow?
Thanks.