title: How to make upload multiple files in single form at Django?
body: <p>I have product form that adding new item for sale in my e-commerce project, It has title, description, price, brand, size areas and I want add area for putting product images, I can upload single file with filefield in form but I need upload multiple files and insert to different relationship file model with my product model (product class gets images from different related class). How can I do it with single form? I think I am so close to solving but I am not right way.</p>
  <p>I used betterforms module but I couldn't it.</p>
  <h1>model.py</h1>
  <pre><code>from django.db import models
  from django.db.models import F
  from ckeditor.fields import RichTextField
  
  class ProductImage(models.Model):
     product_id = models.ForeignKey(&quot;Product&quot;, on_delete = models.CASCADE, verbose_name = &quot;Product&quot;)
     product_image = models.FileField(upload_to='product_images/')
     cover_image = models.BooleanField(default=False)
     upload_date = models.DateTimeField(auto_now_add = True, verbose_name=&quot;Upload Date&quot;)
  
  class Product(models.Model):
     seller = models.ForeignKey(&quot;auth.User&quot;, on_delete = models.CASCADE, verbose_name = &quot;Seller&quot;)
     category = models.ForeignKey(&quot;Category&quot;, on_delete = models.CASCADE, verbose_name = &quot;Category Name&quot;)
     title = models.CharField(max_length = 50, verbose_name=&quot;Product Title&quot;)
     size = models.ForeignKey(&quot;Size&quot;, on_delete = models.CASCADE, verbose_name = &quot;Size&quot;)
     color = models.ForeignKey(&quot;Color&quot;, on_delete = models.CASCADE, verbose_name = &quot;Color&quot;)
     last_used_date = models.DateTimeField(verbose_name=&quot;Last Used Date&quot;)
     description = RichTextField()
     created_date = models.DateTimeField(auto_now_add = True, verbose_name=&quot;Created Date&quot;)
     product_image = models.ManyToManyField(&quot;ProductImage&quot;, verbose_name = &quot;ProductImages&quot;)
  </code></pre>
  <p>I am not sure, It should be manytomanyfield?</p>
  <h1>forms.py</h1>
  <pre><code>from django import forms
  from .models import Product, Category, Size, Color, Brand, Shipping, Comission, ProductImage
  from betterforms.multiform import MultiModelForm
  
  class ProductForm(forms.ModelForm):
      class Meta:
          model = Product
          fields = [&quot;category&quot;,&quot;title&quot;, &quot;description&quot;,&quot;price&quot;, &quot;income&quot;, &quot;active_product&quot;,]
          images = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))
  
  class ProductImageForm(forms.ModelForm):
      class Meta:
          model = ProductImage
          fields = [&quot;cover_image&quot;,&quot;product_image&quot;]
  
  
  class ProductMultiForm(forms.ModelForm):
          form_classes = {
              'product': ProductForm,
              'images': ProductImageForm,
          }
  </code></pre>
  <p>I just tried concatenate two classes.</p>
  <p>There is so complicate.</p>
  <h1>views.py</h1>
  <pre><code>from django.shortcuts import render, HttpResponse, redirect, get_object_or_404
  from .forms import ProductForm
  from django.contrib import messages
  from .models import Product, Category, Size, Color, Brand, Shipping, Comission, ProductImage
  from .forms import ProductMultiForm
  from django.views.generic import UpdateView, CreateView
  from django.http import JsonResponse
  from django.views import View
  
  class ProductMultiForm(request):
      form = ProductMultiForm
      def form_valid(self, form):
          product = form['product'].save()
          images = form['images'].save(commit=False)
          product.seller = request.user
          product.save()
  
          images.save()
          messages.success(CreateView,&quot;Product created successful!&quot;)
          return redirect(&quot;index&quot;)
  
          form = ProductMultiForm(request.POST or None, request.FILES or None)
          product = form.save(commit=False)
          images = form.save(commit=False)
          product.seller = request.user
          images.product_id = product.id
          product.save()
          images.save()
          messages.success(self,&quot;Product created successful!&quot;)
          return redirect(&quot;index&quot;)
  </code></pre>