This commit is contained in:
Sébastien Béal 2012-12-28 05:01:58 -08:00
commit 1b7c79c501

View File

@ -127,6 +127,21 @@ To do any other validation that requires access to multiple fields, add a method
raise serializers.ValidationError("finish must occur after start")
return attrs
from rest_framework import serializers
class QueryParameterSerializer(serializers.Serializer):
start = serializers.DateTimeField(required=False, default='')
stop = serializers.DateTimeField(required=False, default='')
def validate(self, attrs):
"""
Check that the start is before the stop.
"""
if attrs['start'] and attrs['stop']:
if attrs['start'] < attrs['stop']:
raise serializers.ValidationError("Range finish must come after start")
return attrs
## Saving object state
Serializers also include a `.save()` method that you can override if you want to provide a method of persisting the state of a deserialized object. The default behavior of the method is to simply call `.save()` on the deserialized object instance.