Kervvv
Kervvv

Reputation: 845

Save Multiple Files from Django Forms to Model

Looking to upload 2 files into a Django Form using HTML5 (since it supports multi-file upload). The problem I'm facing is it's targets the 1st one for uploading. It knows there are 2 files, because when it saves, it saves twice (as per the for loop below). I thought to use the dictionary to loop over the names, but I receive an error that says this keyword can't be an expression. Maybe this is something simple, but if you need more, I can provide. Just a note, I did not use the forms.py for the file upload, but instead just the regular HTML <input tag. Thanks.

#page.html
<form action="" method="post" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form_a.as_p }}
    <input type="file" name="img" multiple>
    <input type="submit" value="Submit" />
</form>


#models.py
def contact(request):
    if request.method == 'POST':
        form_a = RequestForm(request.POST, request.FILES)
        if form_a.is_valid():
        #assign form data to variables
            saved_first_name = form_a.cleaned_data['First_Name']
            saved_last_name = form_a.cleaned_data['Last_Name']
            saved_department = form_a.cleaned_data['Department']
            saved_attachments = request.FILES.getlist('img')
        #create a dictionary representing the two Attachment Fields
        tel = {'keyword1': 'Attachment_2', 'keyword1': 'Attachment_1'}

        for a_file in saved_attachments:
        #for every attachment that was uploaded, add each one to an Attachment Field
            instance = Model(
                Attachment_1=a_file,
                Attachment_2=a_file
            )
            instance.save()
        all_together_now = Model(First_Name=saved_first_name, Last_Name=saved_last_name, 
            Department=saved_department, Attachment_1=???, Attachment_2=???)
        #save the entire form
        all_together_now.save()
    else:
    #just return an empty form
        form_a = RequestForm()
    return render(request, 'vendor_db/contact.html', {'form_a': form_a})

Upvotes: 3

Views: 3026

Answers (1)

Sazzy
Sazzy

Reputation: 1994

Here is a way that worked for me. I loop each occurrence of an InMemoryUploadedFile in request.FILES and re-assign it back onto request.FILES, then save each one by one.

forms.py

class PhotosForm(forms.ModelForm):
    file = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
    class Meta:
        model = Photos
        fields = ['file']

views.py

def photos(request):
    photos = Photos.objects.all()
    if request.method == 'GET':
        form = PhotosForm(None)
    elif request.method == 'POST':
        for _file in request.FILES.getlist('file'):
            request.FILES['file'] = _file
            form = PhotosForm(request.POST, request.FILES)
            if form.is_valid():
                _new = form.save(commit=False)
                _new.save()
                form.save_m2m()
    context = {'form': form, 'photos': photos}
    return render(request, 'app/photos.html', context)

Upvotes: 7

Related Questions