Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django.utils.timezone returning naive date?

using django 1.4 I have a model with a datetimefield. I imported django.utils.timezone to use as the default value.

from django.utils import timezone
date = models.DateTimeField(default=timezone.now)

however I still receive the warning about DateTimeField received naive date. i have set USE_TZ to true so it should be returning aware datetimes

like image 768
mobiletim Avatar asked Apr 06 '12 05:04

mobiletim


People also ask

What does timezone NOW () return?

timezone. now() useful. This function returns the current date and time as a naive datetime when USE_TZ = False and as an aware datetime when USE_TZ = True .

What is a naive datetime?

We call them "naive" because this datetime representation does not have a time zone. This means the datetime may not actually exist in certain areas in the world even though it is valid. For example, when daylight saving changes are applied by a region, the clock typically moves forward or backward by one hour.

What is the default time zone setting in Django?

Django's timezone is set to UTC by default.


1 Answers

djangos putting in a default date value that isn't tz aware because the field isnt nullable by default. setting null to true means it will just set the date to to NULL instead so the warning isnt raised:

 date = models.DateTimeField(default=timezone.now, null=True)
like image 184
mobiletim Avatar answered Oct 06 '22 08:10

mobiletim