Django DateField default options

PythonDjangoDjango Models

Python Problem Overview


I have a model which has a date time field:

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

When I check the app in the built in django admin, the DateField also has the time appended to it, so that if you try to save it an error is returned. How do I make the default just the date? (datetime.today() isn't working either)

Python Solutions


Solution 1 - Python

This is why you should always import the base datetime module: import datetime, rather than the datetime class within that module: from datetime import datetime.

The other mistake you have made is to actually call the function in the default, with the (). This means that all models will get the date at the time the class is first defined - so if your server stays up for days or weeks without restarting Apache, all elements will get same the initial date.

So the field should be:

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

Solution 2 - Python

Your mistake is using the datetime module instead of the date module. You meant to do this:

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

If you only want to capture the current date the proper way to handle this is to use the auto_now_add parameter:

date = models.DateField(_("Date"), auto_now_add=True)

However, the modelfield docs clearly state that auto_now_add and auto_now will always use the current date and are not a default value that you can override.

Solution 3 - Python

date = models.DateTimeField(default=datetime.now, blank=True)

Solution 4 - Python

I think a better way to solve this would be to use the datetime callable:

from datetime import datetime

date = models.DateField(default=datetime.now)

Note that no parenthesis were used. If you used parenthesis you would invoke the now() function just once (when the model is created). Instead, you pass the callable as an argument, thus being invoked everytime an instance of the model is created.

Credit to Django Musings. I've used it and works fine.

Solution 5 - Python

This should do the trick:

models.DateTimeField(_("Date"), auto_now_add = True)

Solution 6 - Python

You could also use lambda. Useful if you're using django.utils.timezone.now

date = models.DateField(_("Date"), default=lambda: now().date())

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondamonView Question on Stackoverflow
Solution 1 - PythonDaniel RosemanView Answer on Stackoverflow
Solution 2 - PythonEric Palakovich CarrView Answer on Stackoverflow
Solution 3 - PythonandyshiView Answer on Stackoverflow
Solution 4 - PythonperepmView Answer on Stackoverflow
Solution 5 - PythonbtkView Answer on Stackoverflow
Solution 6 - PythonNeilView Answer on Stackoverflow