Categories
Python Answers

How to set Python Django DateField default options?

Spread the love

Sometimes, we want to set Python Django DateField default options.

In this article, we’ll look at how to set Python Django DateField default options.

How to set Python Django DateField default options?

To set Python Django DateField default options, we can set the default argument.

For instance, we write

import datetime

#...

date = models.DateField(_("Date"), default=datetime.date.today)

to create the date field in our model class.

We wrap 'Date' with parentheses to set the default date at the time the class is first defined.

Then we set the default argument to datetime.date.today.

Since we wrapped 'Date' with parentheses, today would be the date at the time the class is first defined.

Conclusion

To set Python Django DateField default options, we can set the default argument.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *