django - Serialize Timezone Object -
i have model has timezone field using django-timezone-field. stores pytz object in field. receive in response object's zone instance.timezone_field.zone
.
with field i'm using readonlymodelviewset , when issuing request, error <dsttzinfo 'us/arizona' lmt-1 day, 16:32:00 std> not json serializable
.
it makes sense why i'm getting error, object not json serializable. how serialize use zone subfield?
to show structure of object field, in shell can zone by:
obj = mymodel.objects.get(id=1) obj.timezone.zone "us/pacific"
i ended making custom serializer field , using zone field on timezone object.
class timezonefield(field): "take timezone object , make json serializable" def to_representation(self, obj): return obj.zone def to_internal_value(self, data): return data class appsettingsserializer(modelserializer): timezone = timezonefield() class meta: model = userappsettings
Comments
Post a Comment