close

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

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How can I set a DateField format in django from the model in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

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

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

    input_formats is a forms.DateField option, not a model.DateField option. You have to set it in your form, not in your models.

  2. set a DateField format in django from the model

    input_formats is a forms.DateField option, not a model.DateField option. You have to set it in your form, not in your models.

Method 1

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

Method 2

input_formats is a forms.DateField option, not a model.DateField option. You have to set it in your form, not in your models.

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read