Created
June 9, 2016 17:59
-
-
Save vaibhav-jain/68dc27261c43dbc225fc70cf35922996 to your computer and use it in GitHub Desktop.
TimeZone Field for DRF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytz | |
import six | |
class TimezoneField(serializers.Field): | |
def to_representation(self, obj): | |
return six.text_type(obj) | |
def to_internal_value(self, time_zone): | |
try: | |
return pytz.timezone(str(time_zone)) | |
except pytz.UnknownTimeZoneError: | |
raise serializers.ValidationError( | |
"Unknown time zone: '%s'" % time_zone | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment