I have a simple model for my csv files:
#models.py
class csvModel(models.Model):
csvFileName = models.CharField(max_length=50)
csvFile = models.FileField(upload_to='tpData/csv/')
My script allows a user to upload a file
Then, I use a ModelChoiceField that allows a user to select one of the uploaded file:
#forms.py
class convertForm(forms.Form):
fileToConvert = forms.ModelChoiceField(queryset=csvModel.objects.all(), label="Choose a CSV file to convert")
When calling is_valid() I can access the value of the ModelChoiceField (e.g. if my csv file is named test1 I will get test1)
#forms.py
def clean_fileToConvert(self):
print(self.cleaned_data.get("fileToConvert")) #I get the name of the field (what I want)
But when I try to access this value just below the is_valid() , I obtain a number (e.g. 48 for a file, 49 for the following etc.)
#views.py
form2 = convertForm(request.POST)
if form2.is_valid():
print(request.POST.get("fileToConvert")) #I get 48
I even tried to return self.cleaned_data.get("fileToConvert") in the clean function but it does not work, I don't know how to access to the selected file name, url etc.