[Django]-Format for DateTimeField

9👍

You can pass the input formats as input_formats argument to DateTimeField, so you can do this

# you can keep a list of formats yourself, or copy from django 1.2 version e.g.
# my_formats = fields.DEFAULT_DATETIME_INPUT_FORMATS + ['%d-%b-%Y']
# for latest django use this
from django.utils.formats import get_format
my_formats = get_format('DATETIME_INPUT_FORMATS')
field = DateTimeField(input_formats=my_formats,...)

If instead you directly want to assign a date-str to models.DateTimeField best way is to just convert it to datetime before hand e.g.

mymodel.date_of_birth = datetime.datetime.strptime("22-DEC-2009", "%d-%b-%Y")

Leave a comment