I am trying to implement a FormView in Django, as it seems to cut down on a bit of boilerplate code. One of the fields is a file upload field.
class LibraryUploadLastStageForm(forms.Form):
technician = forms.ModelChoiceField(label="prepeared by" , queryset=Technician.objects.exclude(has_left=True).order_by('name') , required=True)
subprojects = forms.ModelChoiceField(label="Choose a subproject" , queryset=Subproject.objects.all().order_by('subproject_name') , required=True)
lab_tracking_excel = forms.FileField(label="Choose the Lab Tracking Excel - protocol details associated with these libraries", required=False)
in views.py ,
class LibraryUploadFinalView(FormView):
template_name = 'sequencing/ChooseSomething.html'
form_class = LibraryUploadLastStageForm
def form_valid(self, form):
"""
Update the library technician, and create the librarysubproject links
"""
id_list_str = self.request.GET.get('ids')
ids = map(int , id_list_str.split(','))
libraries = Library.objects.filter(id__in=ids)
print libraries.count()
cd = form.cleaned_data
lib_kwargs = {}
for library in libraries :
library.technician = cd['technician']
if cd['lab_tracking_excel']:
print cd['lab_tracking_excel']
library.lab_tracking_excel = self.request.FILES
library.save()
LibrarySubprojectStats(subproject=cd['subprojects'],
library=library
).save()
print "updated %s" % library.name
return super(LibraryUploadFinalView, self).form_valid(form)
The problem is when I am in the FromView form_valid() method (where I am saving the details), seld.request.FILES is empty when I check it in the debugger.
update: I can see the file is stored under form.data, but how do I get it from there to form.cleaned_data - which is empty?