Skip to content
Advertisement

Django model DateTimeField set auto_now_add format or modify the serializer

I have this field in my model:

JavaScript

And it is saved with this format:

2016-05-18T15:37:36.993048Z

So I would like to convert it to this format DATE_INPUT_FORMATS = ('%d-%m-%Y %H:%M:S') but I dont know where to do it.

I have a simple serializer class, could I override it to modify the format? or maybe create a get_date() model method?

JavaScript

My settings:

JavaScript

Advertisement

Answer

Set DATETIME_FORMAT in your settings.py as specified here.

The default formatting to use for displaying datetime fields in any part of the system. Note that if USE_L10N is set to True, then the locale-dictated format has higher precedence and will be applied instead

The date part of your settings.py should afterwards look like so:

JavaScript

Alternatively, you can manually change formats after retrieval by doing:

JavaScript

This conversion can be added to your serializer or model as your proposed get_date() method

Advertisement