title: Django REST Framework custom fields validation
body: <p>I am trying to create custom validation for a model, to check that its <code>start_date</code> is before its <code>end_date</code> and it is proving near impossible.</p>
  
  <p>Stuff I've tried:</p>
  
  <ul>
  <li><p>built-in Django validators: none check for this</p></li>
  <li><p>writing my own, like so:</p>
  
  <pre><code>def validate_date(self):
     if self.start_date &lt; self.end_date:
          raise serializers.ValidationError("End date must be after start date.")
  </code></pre></li>
  </ul>
  
  <p>That bit of code I have added to the Serializer class (and then the model), but it does not seem to get called in either location.</p>
  
  <p>I also found <a href="https://stackoverflow.com/questions/26119672/django-rest-framework-field-level-validation-in-serializer">this</a> bit of code that might be of use, but I don't know how to integrate in my method- it seems that it would work to validate one model attribute, but I need to check between two attributes.</p>
  
  <p>My model:</p>
  
  <pre><code>class MyModel(models.Model):
  
      created = models.DateTimeField(auto_now_add=True)
      relation_model = models.ForeignKey(RelationModel, related_name="mymodels")
      priority = models.IntegerField(
          validators = [validators.MinValueValidator(0), validators.MaxValueValidator(100)])
      start_date = models.DateField()
  end_date = models.DateField()
  
      @property
      def is_active(self):
          today = datetime.date.today()
          return (today &gt;= self.start_date) and (today &lt;= self.end_date)
  
      def __unicode__(self):
          ...
  
      class Meta:
          unique_together = ('relation_model', 'priority', 'start_date', 'end_date')
  </code></pre>
  
  <p>Fyi, all the other validations work!</p>
  
  <p>My serializer:</p>
  
  <pre><code>class MyModelSerializer(serializers.ModelSerializer):
  
      relation_model = RelationModelSerializer
      is_active = serializers.Field(source='is_active')
  
      def validate_date(self):
          if self.start_date &gt; self.end_date:
              raise serializers.ValidationError("End date must be after start date.")   
  
      class Meta:
          model = MyModel
          fields = (
              'id', 'relation_model', 'priority', 'start_date', 'end_date', 'is_active'
          )
  </code></pre>
  
  <p>My view:</p>
  
  <pre><code>class MyModelList(generics.ListCreateAPIView):
      permission_classes = (IsAdminUser,)
      queryset = MyModel.objects.all()
      serializer_class = MyModelSerializer
      ordering = ('priority')
  </code></pre>