42👍
✅
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")
12👍
After testing the accepted answer, I got the following error:
AssertionError: Expected a
date
, but got adatetime
. Refusing to coerce, as this may mean losing timezone information. Use a custom read-only field and deal with timezone issues explicitly.
Using the serializer field as datetime
solved the issue.
class StartListSerializer(serializers.Serializer):
# ...
time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")
- [Django]-Allowing RabbitMQ-Server Connections
- [Django]-PyCharm: DJANGO_SETTINGS_MODULE is undefined
- [Django]-Getting Values of QuerySet in Django
Source:stackexchange.com