[Answered ]-Specify time format in model_to_dict

1πŸ‘

βœ…

I receive timestamp in the following format "timestamp": "2021-06-27T14:18:00Z"

This is an ISO 8601 datetime format. We can parse this with the python-dateutil package. You can install this package with:

pip3 install python-dateutil

then we can parse this with:

>>> from dateutil.parser import isoparse
>>> isoparse('2021-06-27T14:18:00Z')
datetime.datetime(2021, 6, 27, 14, 18, tzinfo=tzutc())

You thus can pre-process the dictionary with:

from dateutil.parser import isoparse

mydata['timestamp'] = isoparse(mydata['timestamp'])

If you want to do advanced serializing and deserializing, it might be better to work with the serializers of the Django REST framework.

Leave a comment