You should use an object wide validation (validate()
), since validate_date
will never be called since date
is not a field on the serializer. From the documentation:
class MySerializer(serializers.ModelSerializer):
def validate(self, data):
"""
Check that the start is before the stop.
"""
if data['start_date'] > data['end_date']:
raise serializers.ValidationError("finish must occur after start")
return data
As suggested by Michel Sabchuk you can add the validation error to the end_date
field:
class MySerializer(serializers.ModelSerializer):
def validate(self, data):
"""
Check that the start is before the stop.
"""
if data['start_date'] > data['end_date']:
raise serializers.ValidationError({"end_date": "finish must occur after start"})
return data
Another possibility is to create a validator. I created one based on the code for UniqueTogetherValidator
:
from rest_framework.utils.representation import smart_repr
class DateBeforeValidator:
"""
Validator for checking if a start date is before an end date field.
Implementation based on `UniqueTogetherValidator` of Django Rest Framework.
"""
message = _('{start_date_field} should be before {end_date_field}.')
def __init__(self, start_date_field="start_date", end_date_field="end_date", message=None):
self.start_date_field = start_date_field
self.end_date_field = end_date_field
self.message = message or self.message
def __call__(self, attrs):
if attrs[self.start_date_field] > attrs[self.end_date_field]:
message = self.message.format(
start_date_field=self.start_date_field,
end_date_field=self.end_date_field,
)
# Replace the following line with
# raise serializers.ValidationError(
# {self.end_date_field: message},
# code="date_before",
# )
# if you want to raise the error on the field level
raise serializers.ValidationError(message, code="date_before")
def __repr__(self):
return '<%s(start_date_field=%s, end_date_field=%s)>' % (
self.__class__.__name__,
smart_repr(self.start_date_field),
smart_repr(self.end_date_field)
)
class MySerializer(serializers.ModelSerializer):
class Meta:
# If your start/end date fields have another name give them as kwargs tot the
# validator:
# DateBeforeValidator(
# start_date_field="my_start_date",
# end_date_field="my_end_date",
# )
validators = [DateBeforeValidator()]
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.
class MyModel(models.Model):
start_date = models.DateField()
end_date = models.DateField()
def clean(self):
if self.end_date < self.start_date:
raise ValidationError("End date must be after start date.")