How can I set a DateField format in django from the model?

后端 未结 3 1009
一向
一向 2020-12-13 14:20

I am creating a django application and I have the next problem: this error is shown when I want to set a date:

ValidationError [u\"\'12/06/2012\' value has a         


        
3条回答
  •  天涯浪人
    2020-12-13 15:01

    As @bruno as mentioned in his answer, input_formats is a forms field, however it can be used to control the date format saved from the model.

    In settings.py set DATE_INPUT_FORMATS as below:

    DATE_INPUT_FORMATS = ['%d-%m-%Y']
    

    And in your form you could do something like below:

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

提交回复
热议问题