7

I have a model, which has a date field

date_of_birth = models.DateField(blank=True, null=True, verbose_name="DOB")

I would like to format it to save dates in the format dd/MM/yyyy, but everything I have tried fails.

I think the default must be YYYY-MM-dd because that is how it saves to my database. Trying to submit dates in a different format gives the error:

[u"'17/01/1970' value has an invalid date format. It must be in YYYY-MM-DD format."]

I have tried using Date Widgets but am having a few issues getting it to be compatible with my models.py

Jon
  • 3,174
  • 11
  • 39
  • 57

4 Answers4

8

You can change this by overriding input_formats on the DateField in your form. This is covered pretty well in the documentation. The default date formats are

['%Y-%m-%d',      # '2006-10-25'
'%m/%d/%Y',       # '10/25/2006'
'%m/%d/%y']       # '10/25/06'

or

['%Y-%m-%d',      # '2006-10-25'
'%m/%d/%Y',       # '10/25/2006'
'%m/%d/%y',       # '10/25/06'
'%b %d %Y',      # 'Oct 25 2006'
'%b %d, %Y',      # 'Oct 25, 2006'
'%d %b %Y',       # '25 Oct 2006'
'%d %b, %Y',      # '25 Oct, 2006'
'%B %d %Y',       # 'October 25 2006'
'%B %d, %Y',      # 'October 25, 2006'
'%d %B %Y',       # '25 October 2006'
'%d %B, %Y']      # '25 October, 2006'

Depending on whether or not you are using localization in your settings.

You appear to be looking for %d/%m/%y, which isn't a default format.

Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
  • 1
    What?! Django only accepts these formats? What about %d/%m/%Y which is used in most of the world? – Joris de Ruiter Jan 03 '17 at 18:56
  • 2
    This only applies to the forms DateField, notice that the OP specified models.DateField. The models.DateField to_python method invokes the utils.dateparse parse_date method, which only allows %Y-%m-%d. – ucbpaladin Dec 29 '17 at 18:57
  • The OP mentioned the Date widgets, which was enough to assume that the issue was specific to a ModelForm and not a Model itself. I do appreciate your answer though, because not everyone is going to hit this question because that got the error from using a form. – Kevin Brown-Silva Jan 01 '18 at 03:08
1

One option would be to extend DateField to handle other date formats, such as by converting them yourself during the save, instructions here. For reference, I just had to deal with this problem and my solution looked like this:

class HWDateField(models.DateField):
def to_python(self, value):
    if value is None:
        return value
    return super(HWDateField,self).to_python(parseDate(value))

Where parseDate was a conversion function I had already written.

ucbpaladin
  • 111
  • 4
0

You can not set the input_formats on the model's field, but only on the form's field. With a ModelForm, and fields auto generated based on the model, you redefine/customize what you need as documented here.

"[...] you want complete control over of a field, you can do this by declaratively specifying fields like you would in a regular Form.". Here's my example :

class UserCreationForm(BaseUserCreationForm):
    birth_date = forms.DateField(input_formats=['%d/%m/%Y'])

    class Meta(BaseUserCreationForm.Meta):
        model = User
        fields = ("username", "first_name", "last_name", "email", "birth_date")
Tam
  • 84
  • 1
  • 6
0

Simply setting the timezone/locale in settings will adjust datetime widget to correct format.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253