Search code examples
pythondjangodjango-formsdate-formatdjango-settings

Django date format 'dd-mm-yyyy'


Is there any way I can get django to store my data in postgresql as 'dd-mm-yyyy' (if required) and have the django forms validate for 'dd-mm-yyyy'?

(I have tried without much success things like:

DATE_INPUT_FORMATS = ('%d-%m-%Y')
USE_I18N = True
USE_L10N = True

And done a lot of googling but no success :(


Solution

  • The problem turned out to be that I needed both the ISO and UK date formats in the settings.py file like so:

    DATE_INPUT_FORMATS = ('%d-%m-%Y','%Y-%m-%d')
    

    and then the forms.py adjusted to the following:

    class ClientDetailsForm(ModelForm):
        date_of_birth = DateField(input_formats=settings.DATE_INPUT_FORMATS)
        class Meta:
            model = ClientDetails
    

    Why USE_L10N = True can't just do this I don't know!

    [Thanks to this and this]