From 3f0da7b961a006d06c2d8ff5e396e37f314001ed Mon Sep 17 00:00:00 2001 From: Alexander Karpov <51019345+Alexander-D-Karpov@users.noreply.github.com> Date: Sat, 3 Jun 2023 17:21:04 +0300 Subject: [PATCH] fixed multiple file upload --- akarpov/common/forms.py | 18 ++++++++++++++++++ akarpov/music/forms.py | 5 ++++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 akarpov/common/forms.py diff --git a/akarpov/common/forms.py b/akarpov/common/forms.py new file mode 100644 index 0000000..07a650c --- /dev/null +++ b/akarpov/common/forms.py @@ -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 \ No newline at end of file diff --git a/akarpov/music/forms.py b/akarpov/music/forms.py index 0dc7fc6..3d61ffd 100644 --- a/akarpov/music/forms.py +++ b/akarpov/music/forms.py @@ -1,9 +1,12 @@ from django import forms +from akarpov.common.forms import MultipleFileField + class TracksLoadForm(forms.Form): address = forms.CharField(max_length=500) + class FileUploadForm(forms.Form): - file = forms.FileField(widget=forms.ClearableFileInput(attrs={"multiple": True})) + file = MultipleFileField()