Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
701 views
in Technique[技术] by (71.8m points)

serialization - how to format time in django-rest-framework's serializer?

there is my serializer code:
serializer.py

class StartListSerializer(serializers.Serializer):
    id = serializer.CharField()
    time = serializers.DateField(fromat=None, input_formats=None) 

it will give me this:
'time': '2014-03-14T22:44:16.923000'
the input time format is datetime.datetime(2014, 3, 14, 22, 44, 37, 280000)
But I want to get time like this 2014-03-14 22:44:37
how can I change my serializer to get this time?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The usual Python datetime format will work:

# for DateField
date = serializers.DateField(format="%Y-%m-%d")

# for DateTimeField
time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...