[Answered ]-Django changing date format is not working

1👍

The DATE_FORMAT is not about the model layer, only the forms, serializers, etc. So you can not construct a DateModel object with this.

It is here not necessary anyway, you can just pass a date object:

from appname import models, serializer
from django.utils import timezone
from rest_framework import views


class DateView(views.APIView):
    serializer_class = serializer.DateSerializer

    def get(self, request):
        return Response(
            serializer.DateSerializer(models.DateModel.objects.all()).data,
            status=200,
        )

    def post(self, request):
        data = serializer.DateSerializer(data=request.data)
        if data.is_valid():
            desc = data.data['desc']
            model_instance = models.DateModel.objects.create(
                desc=desc, date=timezone.now().date()
            )
            return Response('Posted!', status=200)
        return Response(
            'Invalid Data, 400 status code error is raised', status=400
        )

A database stores a timestamp usually as a binary format. The string format is thus "lost" when you store it. DATE_FORMAT only specifies how Django "talks" to the outside world, so in templates, forms, serializers, etc.

Leave a comment