My field in model:
created_at = models.DateTimeField(auto_now_add=True)
it returns: "created_at": "2023-02-21T09:24:55.814000Z"
how can i get this one (without last part): "created_at": "2023-02-21T09:24:55"
Is there another way instead of this?
(part from the django rest framework serializer)
representation['created_at'] = instance.created_at.split('.')[0]
>Solution :
You can use strftime method to format the datetime in the way you want:
created_at.strftime("%Y-%m-%dT%H:%M:%S")
# Should give "2023-02-21T09:24:55"
Generically in your APIs, ISO-8601 format should be preferred. You can use created_at.isoformat() to get the string representation in that format.