Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django rest framework timefield input format

After hours of searching, I found many posts that are related but wasn't able to help.

What I want to do is input eg: 10:30 AM into the TimeField.
In the django rest framework API on the browser, it is using this 10:30 AM format ('%I:%M %p').
But when I am using postman to test it, the output is in 24hr format ('%H:%M:%S'). I also tried to use 10:30 PM as input but the output I get is 10:30:00 instead of 22:30:00.

Many of the answers I found suggest to change the TimeField format in settings.py by using this line:

TIME_INPUT_FORMATS = ('%I:%M %p',)

but it doesn't work for me.

Sorry for my inexperience on django rest framework as I am still learning.

Here is the screenshot of the result. On browser API:

On browser API

On postman:

On postman

like image 911
Jin Nii Sama Avatar asked Nov 23 '17 07:11

Jin Nii Sama


People also ask

How do I change the date format in Django REST Framework?

Or you can write custom field and use it instead of default. class UserSerializer(serializers. ModelSerializer): created_at = serializers. DateTimeField(format='%Y') # add read_only=True to the field in case if date created automatically ...

How do you pass extra context data to Serializers in Django REST Framework?

In function based views we can pass extra context to serializer with "context" parameter with a dictionary. To access the extra context data inside the serializer we can simply access it with "self. context". From example, to get "exclude_email_list" we just used code 'exclude_email_list = self.

What is Read_only true in Django?

Any 'read_only' fields that are incorrectly included in the serializer input will be ignored. Set this to True to ensure that the field is used when serializing a representation, but is not used when creating or updating an instance during deserialization.

What is Default_authentication_classes?

DEFAULT_AUTHENTICATION_CLASSES. A list or tuple of authentication classes, that determines the default set of authenticators used when accessing the request.


1 Answers

If you check the documentation on the TimeField you will see:

Signature: TimeField(format=api_settings.TIME_FORMAT, input_formats=None)

Where

format - A string representing the output format. If not specified, this defaults to the same value as the TIME_FORMAT settings key, which will be 'iso-8601' unless set. Setting to a format string indicates that to_representation return values should be coerced to string output. Format strings are described below. Setting this value to None indicates that Python.

input_formats - A list of strings representing the input formats which may be used to parse the date. If not specified, the TIME_INPUT_FORMATS setting will be used, which defaults to ['iso-8601'].

So you either can specify the format and input_formats on the serializer, or set the settings.TIME_FORMAT and settings.TIME_INPUT_FORMATS.

Let's set the first case:

class MySerializer(serializers.Serializer):
    ...
    birthTime=serializers.TimeField(format='%I:%M %p', input_formats='%I:%M %p')

Some suggestions:

  1. Make your variable names snake case: birth_time.
  2. You may need to play a bit around with the input format because you may expect many different inputs:

    input_formats=['%I:%M %p','%H:%M',...]
    
like image 137
John Moutafis Avatar answered Sep 27 '22 02:09

John Moutafis