I am trying to create custom validation for a model, to check that its start_date
is before its end_date
and it is proving near impossible.
Stuff I've tried:
built-in Django validators: none check for this
writing my own, like so:
def validate_date(self): if self.start_date < self.end_date: raise serializers.ValidationError("End date must be after start date.")
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.
I also found this 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.
My model:
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 >= self.start_date) and (today <= self.end_date)
def __unicode__(self):
...
class Meta:
unique_together = ('relation_model', 'priority', 'start_date', 'end_date')
Fyi, all the other validations work!
My serializer:
class MyModelSerializer(serializers.ModelSerializer):
relation_model = RelationModelSerializer
is_active = serializers.Field(source='is_active')
def validate_date(self):
if self.start_date > 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'
)
My view:
class MyModelList(generics.ListCreateAPIView):
permission_classes = (IsAdminUser,)
queryset = MyModel.objects.all()
serializer_class = MyModelSerializer
ordering = ('priority')
You should use an object wide validation (
validate()
), sincevalidate_date
will never be called sincedate
is not a field on the serializer. From the documentation:Pre DRF 3.0 you could also add it to the clean function of a model, but this is not called anymore in DRF 3.0.
jgadelange's answer worked before django rest 3 probably. If any one using the django rest framework 3* version, I think this would be helpful for that folk. one should keep validation process in model level and clean method may be the one solution. But django rest framework announcement says here that, if someone wants to validate rest-call in model .clean method, he/she should override the serializer validate method and need to call the clean method form this serializer class by the following way
(because doc says : clean() method will not be called as part of serializer validation)
and model
Another answer here might be useful, regarding situation if one chooses to override serializer's
validate()
method.Regarding answer on Order of Serializer Validation in Django REST Framework, I must say that
serializer.validate()
method is called at the end of validation sequence. However, field's validators are called before that, inserializer.to_internal_value()
, raisingValidationError
at the end.This means that custom validation errors do not stack with default ones.
In my opinion cleanest way to achieve desired behaviour is by using target field method validation in serializer class:
In case if you need another field value from model, such as
start_date
in this case, you can get them (yet unvalidated, as process is not complete) with:In case anyone struggling with implementing this as class-based validator on field...
Assuming you have
start_date
andend_date
fields in your serializer, you can then set in onend_date
field withvalidators=[EndDateValidator('start_date')]
.