Does Django.utils.timezone return a naive date?

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

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

however, I still get a DateTimeField date date warning. I set USE_TZ to true so that it returns knowing dates

+7
source share
4 answers

djangos puts a default date value that tz does not know, because the default value of a field is null. setting null to true means that it simply sets the date to NULL so that the warning is not raised:

  date = models.DateTimeField(default=timezone.now, null=True) 
+2
source

In my case, I kept getting the question when I ran the tests. This is due to a past migration that incorrectly used the date and time instead of the time zone. I returned to the migration before it triggered a warning, deleted it, ran makemigrations again. This solved the problem for me.

0
source

now()

Returns a knowing or naive datetime that represents the current point in time USE_TZ is True or False respectively.

https://docs.djangoproject.com/en/dev/ref/utils/#django.utils.timezone.now

-one
source

I know this is a problem from the outside, but have you tried auto_now_add=True in your field? No need to use default for what you need.

On the other hand, your version should work if you really have USE_TZ=True .

I would say that it works, and you get RunTimeWarning from somewhere where you set the date field directly, and not by default. Try narrowing down when a warning is triggered.

-one
source

All Articles