Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can not change the date format in django drf

I have drf model which is containe DateField. That field default format is "YYYY-MM-DD" just i want to convert "DD-MM-YYYY" how can is possible.

from rest_framework import serializers
from.models import SpaUser
from djoser.serializers import UserCreateSerializer as BaseUserRegistrationSerializer
import datetime
from rest_framework.settings import api_settings



class SpaUserSerializer(serializers.ModelSerializer):

    date_joined = serializers.ReadOnlyField()
    birthdate = serializers.DateField(format="%d-%m-%Y", input_formats=['%d-%m-%Y',])
like image 577
user9714967 Avatar asked Jun 02 '18 19:06

user9714967


1 Answers

If it is universal, in your settings file add "DATE_INPUT_FORMATS" to REST_FRAMEWORK settings like:

REST_FRAMEWORK = {
    "DATE_INPUT_FORMATS": ["%d-%m-%Y"],
    ...
}

for more details check http://www.django-rest-framework.org/api-guide/settings/#date-and-time-formatting

like image 103
SamwelOpiyo Avatar answered Oct 17 '22 00:10

SamwelOpiyo