I'm working on an existing codebase which implements an api using Django and the django-rest-framework. When you post a datetime like this:
2019-06-21T10:35:46+02:00
it is stored in UTC as 2019-06-21 08:35:46+00
(as expected). This is because I've got USE_TZ = True
.
When I serve the data, I also want it to be converted to the localised format again (2019-06-21T10:35:46+02:00
). So following this tip I implemented it like this:
class DateTimeFieldWihTZ(serializers.DateTimeField):
""" Class to make output of a DateTime Field timezone aware """
def to_representation(self, value):
value = timezone.localtime(value)
return super(DateTimeFieldWihTZ, self).to_representation(value)
class PeopleMeasurementSerializer(HALSerializer):
class Meta:
model = PeopleMeasurement
fields = [
'_links',
'id',
'timestamp',
'sensor',
'count'
]
timestamp = DateTimeFieldWihTZ(format='%Y-%m-%d %H:%M:%S')
But this serves it as 2019-06-21 08:35:46
. How can I serve it as 2019-06-21T10:35:46+02:00
again?