我一直试图解决这个问题已有一段时间了。请尽可能帮助......
我有一个REST框架响应,其中一个日期时间值是UTC格式。我想将此格式更改为更友好的格式。
休息框架输出:
{
"data": [
{
"id": "1",
"start_time": "2017-12-28T12:56:55-08:00",
},
{
"id": "2",
"start_time": "2017-12-28T12:14:10-08:00",
},
{
"id": "3",
"start_time": "2017-12-28T09:37:35-08:00",
},
]
}
观点:(我的代码更改显示中的start_time格式)
serializer = ChangeLogSerializer(changelog['items'], many=True)
for log in serializer.data:
serializer.data[log]['start_time'] = log['start_time'].strftime('%Y-%m-%d %T')
serializers.py
class ChangeLogSerializer(serializers.ModelSerializer):
"""Serializer to map the Model instance into JSON format."""
class Meta:
"""Meta class to map serializer's fields with the model fields."""
start_time = serializers.DateField(format="%y-%m-%d %H:%M:%S")
model = ChangeLog
fields = ('id', 'sku', 'serial_num', 'mac', 'sw_ver',
'state', 'outcome', 'logfile', 'process',
'cm', 'site', 'port_num', 'term_srvr',
'start_time', 'end_time')
read_only_fields = ('start_time', 'end_time')
观察:
答案 0 :(得分:1)
找到答案,在settings.py文件中更改:
REST_FRAMEWORK = {
'DATETIME_FORMAT': "%Y-%m-%d - %H:%M:%S",
}