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

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

ValidationError [u"'12/06/2012' value has an invalid date format. It must be in YYYY-MM-DD format."] 

For this model:

 class ModelA(models.Model): date1 = models.DateField(null=True) date2 = models.DateField(null=True) 

How to set DateField %m/%d/%Y format.

The "input_formats" option is not recognized.

Thanks!

+7
python django format
source share
2 answers

In settings.py set DATE_INPUT_FORMATS as shown below:

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

And in your form, you can do something like below:

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

input_formats is a forms.DateField parameter, not a model.DateField parameter. You should install it in your form, not in your models.

+12
source share

All Articles