I'm pretty new to Django and I'm working on a project in which I have to automate PDF file uploads from a given folder to a model in Django and these files will undergo a text extraction process. I wrote a script to monitor the folder and upload new files to database.
The django model looks like this:
class Version(models.Model):
id = models.AutoField(primary_key = True)
file = models.FileField(upload_to = history_directory_path, db_column = 'file', max_length = 500)
filename = models.CharField(default = '', max_length = 100, db_column = 'filename')
date = models.DateTimeField(auto_now_add = True, db_column = 'date)
version_num = models.IntegerField(null = True, blank = True)
history_directory_path is a function which creates the folder using the filename and its version number. A piece of my automatic upload script (in this example only for 1 file, I will put it in a loop when it starts working) is the following:
from django.core.files import File
from myapp.models import Version
import os
import glob
BASE_PATH = r'C:\path\to\folder_to_be_monitored'
files = [os.path.basename(x) for x in glob.glob(BASE_PATH + '/*.pdf')]
filename = files[0]
f = open(os.path.join(BASE_PATH, filename), 'rb')
my_file = File(f)
version = Version(file = my_file, filename = filename, version_num = 1)
version.save()
executing the last command I get
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:\\Users\\user_name\\PycharmProjects\\project\\project\\media\\documents\\history\\filename\\version_0\\C:'
I have read lots of questions about saving files to filefield in django (and also read the related documentations) but I couldn't make a working solution for it. And I don't understand how C: has gotten to end of the path in the error message.
Can you please provide me some directions or point out where did I went wrong?