Skip to content

Instantly share code, notes, and snippets.

@vaibhav-jain
Created June 9, 2016 17:59
Show Gist options
  • Save vaibhav-jain/68dc27261c43dbc225fc70cf35922996 to your computer and use it in GitHub Desktop.
Save vaibhav-jain/68dc27261c43dbc225fc70cf35922996 to your computer and use it in GitHub Desktop.
TimeZone Field for DRF
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