Is it possible to use DRF's datetime picker and include the UTC offset for timestamps in the response JSON?

I am trying to use the Web browsable API in Django Rest Framework. I have objects with timestamp data in this format, "2025-05-08T22:02:58.869000-07:00". But the required format for Django's datetime picker does not support UTC offset or the last three significant digits for microseconds as shown below:

enter image description here

I can trim the input data to satisfy the datetime picker like this:

class MillisecondDateTimeField(serializers.DateTimeField):
    def to_representation(self, value):
        return value.astimezone().strftime("%Y-%m-%dT%H:%M:%S") + ".{:03d}".format(
            int(value.microsecond / 1000)
        )

But then the UTC offset is also missing from the timestamp in the POST/PUT response.

{
   ...
   "start_datetime": "2025-05-08T22:02:58.869",
   ...
}

How can I make the value for "start_datetime" appear as "2025-05-08T22:02:58.869000-07:00" in the JSON response from POST/PUT while also using "2025-05-08T22:02:58.869" in the datetime picker? I have been researching online and trying for days.

Вернуться на верх