1👍
But it returns a datetime object as the date. How can I transform it to ‘YYYY-MM-DD’ format?
Please, don’t use the database to format dates or strings. Databases should not be used to render data. Databases are used to store, retrieve and aggregate data.
It is the task of the templates, serializers, etc. to provide the data in an accessible format. For example in the template, you can format a date with the |date
template filter [Django-doc]:
{{ my_date|date:"Y-m-d" }}
Django also can serialize data [Django-doc], if you serialize it to JSON, it will also encod it in that format:
>>> json.dumps(date(2023, 10, 20), cls=DjangoJSONEncoder)
'"2023-10-20"'
Source:stackexchange.com