title: Django: form_valid and model validation
body: <p>I am creating a car reservation application in Django. Users can create a Reservation for a given Car. This happens at <code>/reservation/&lt;car_id&gt;/add</code>. This form does not contain the Car field because the id is given in the URL. Now I want to add a validation to the Reservation model so no reservations can overlap. I have the following code (minimal version):</p>
  
  <pre class="lang-py prettyprint-override"><code># models.py
  
  class Car(models.Model):
      name = models.CharField(max_length=200)
  
  
  class Reservation(models.Model):
      car = models.ForeignKey(Car, on_delete=models.CASCADE)
      owner = models.ForeignKey(User, on_delete=models.CASCADE)
      start_time = models.DateTimeField()
      end_time = models.DateTimeField()
  
      def clean(self):
          # Check if overlaps with other reservations in self.car.reservation_set
  
  # views.py
  
  class ReservationAdd(LoginRequiredMixin, CreateView):
      template_name = 'reservation/reservation_add.html'
      model = Reservation
      form_class = ReservationAddForm
  
      def form_valid(self, form):
          form.instance.car = Car.objects.get(pk=self.kwargs['car_id'])
          form.instance.owner = self.request.user
          return super().form_valid(form)
  
  # forms.py
  
  class ReservationAddForm(forms.ModelForm):
  
      class Meta:
          fields = ('start_time', 'end_time')
          model = Reservation
  </code></pre>
  
  <p>Now I get the following error at the <code>Reservation.clean</code> method:</p>
  
  <pre><code>reservation.models.Reservation.car.RelatedObjectDoesNotExist: Reservation has no car.
  </code></pre>
  
  <p>It seems to me that the <code>clean</code> method gets called before the <code>form_valid</code> method. So my design is not entirely correct I think. What is the correct way to do this?</p>