There is, at the moment of writing, no FileField
for a model that can store multiple files. Usually this is modelled with an extra model, for example CompanyDoc
that has a FileField
and a ForeignKey
to the Applicant
model, so:
class Applicant(models.Model):
# …
pass
class CompanyDoc(models.Model):
applicant = models.ForeignKey(
Applicant, on_delete=models.CASCADE
)
file = models.FileField(upload_to='company_doc/')
You can then construct a form that can upload multiple files with:
from django import forms
class CompanyDocsForm(forms.Form):
files = forms.FileField(
widget=forms.ClearableFileInput(attrs={'multiple': True})
)
and then process this with:
def some_view(request, applicant_id):
if request.method == 'POST':
form = CompanyDocForm(request.POST, request.FILES)
if form.is_valid():
data = [
CompanyDoc(file=file, applicant_id=applicant_id)
for file in request.FILES.getlist('files')
]
CompanyDoc.objects.bulk_create(data)
else:
# …
pass
else:
# …
pass
# …
For more information, see the Uploading multiple files section of the documentation.