How to get files from a multiple file `FileField` from each form in a `FormSet`

I’m adding a feature to our data submission page that uses a formset to take multiple of a repeated form that’s dynamically generated using javascript. And I’m changing a simple file form field to a multiple file form field, and that multiple file input will occur in each individual repeated form. At least, that’s the idea.

The old form class had:

class BuildSubmissionForm(Form):
    peak_annotation_file = FileField(
        required=False,
        widget=ClearableFileInput(attrs={"multiple": False}),
    )

The old form_valid() method had:

for index, rowform in enumerate(formset.cleaned_data):
    peak_annotation_file = rowform.get("peak_annotation_file")

Now the form class has:

class BuildSubmissionForm(Form):
    peak_annotation_file = FileField(
        required=False,
        widget=ClearableFileInput(attrs={"multiple": True}),
    )

But I haven’t been able to find anything that says this will work in the form_valid method. (I’m not to a point yet where the I’m ready to start debugging.)

for index, rowform in enumerate(formset.cleaned_data):
    peak_annotation_files = rowform.get("peak_annotation_files")

All I have seen are references to things like:

self.request.FILES.getlist("peak_annotation_files")

but that doesn’t seem like it could give me the files from any one form instance in the formset.

Is this not possible? Do I need to implement some javascript to map out which files belong to which form? And javascript to add selected files to a single hidden multiple file form field?

A formset is a container for a set of forms. You should be able to iterate over the individual forms and retrieve the files associated with the field for each form. (In other words, don’t enumerate over formset.cleaned_data, iterate over formset.forms.)