title: Is there a better way to write this code so DRY can be maintained?
body: <p>I have 2 models</p>
  <ol>
  <li><p>Tour to save Tour</p>
  </li>
  <li><p>TourImage to save images related to Tour</p>
  <pre><code>class Tour(models.Model):
      name = models.CharField(max_length=50)
  
  class TourImage(models.Model):
      tour = models.ForeignKey(Tour, on_delete=models.CASCADE)
      image = models.FileField(upload_to='images/')
  </code></pre>
  </li>
  </ol>
  <p>In my views.py file I have the following code to save the Tour and the tour images</p>
  <pre><code>class CreateTourView(CreateView):
      model = Tour
      template_name = &quot;create_tour.html&quot;
      fields = &quot;__all__&quot;
  
      def form_valid(self, form):
          tour = form.save()
          for image in self.request.FILES.getlist(&quot;extra_images&quot;):
              TourImage.objects.create(tour=tour, image=image)
      return super().form_valid(form)
  </code></pre>
  <p>The HTML form looks like this</p>
  <pre><code>&lt;form action=&quot;&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt;
  {% csrf_token %}
  {{ form | crispy }}
  &lt;input type=&quot;file&quot; name=&quot;extra_images&quot; multiple /&gt;
  &lt;input type=&quot;submit&quot; value=&quot;Save&quot; /&gt;
  &lt;/form&gt;
  </code></pre>
  <p>This works fine, the images are saved the Database.</p>
  <p>But I also want to add option to edit Tour and add extra images, so the UpdateView code is as follows</p>
  <pre><code>class TourUpdateView(UpdateView):
      model = Tour
      template_name = &quot;update_tour.html&quot;
      fields = &quot;__all__&quot;
  
      def form_valid(self, form):
          tour = form.save()
          for image in self.request.FILES.getlist(&quot;extra_images&quot;):
              TourImage.objects.create(tour=tour, image=image)
          return super().form_valid(form)
  </code></pre>
  <p>The <code>.form_valid()</code> method is same for both CreateView and UpdateView.</p>
  <p>Is there a better way to code this so as to avoid repetitive code?</p>