jartymcfly jartymcfly - 1 year ago 316
Python Question

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

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 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 can I set the DateField format to be

The option
is not recognized.

Thank you!

Answer Source

In settings.py set DATE_INPUT_FORMATS as below:


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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download