fixed multiple file upload

This commit is contained in:
Alexander Karpov 2023-06-03 17:21:04 +03:00 committed by GitHub
parent ff080e3354
commit 3f0da7b961
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

18
akarpov/common/forms.py Normal file
View File

@ -0,0 +1,18 @@
from django import forms
class MultipleFileInput(forms.ClearableFileInput):
allow_multiple_selected = True
class MultipleFileField(forms.FileField):
def __init__(self, *args, **kwargs):
kwargs.setdefault("widget", MultipleFileInput())
super().__init__(*args, **kwargs)
def clean(self, data, initial=None):
single_file_clean = super().clean
if isinstance(data, (list, tuple)):
result = [single_file_clean(d, initial) for d in data]
else:
result = single_file_clean(data, initial)
return result

View File

@ -1,9 +1,12 @@
from django import forms from django import forms
from akarpov.common.forms import MultipleFileField
class TracksLoadForm(forms.Form): class TracksLoadForm(forms.Form):
address = forms.CharField(max_length=500) address = forms.CharField(max_length=500)
class FileUploadForm(forms.Form): class FileUploadForm(forms.Form):
file = forms.FileField(widget=forms.ClearableFileInput(attrs={"multiple": True})) file = MultipleFileField()