Django auto_now_add=True but "received a naive datetime"

Viewed 562

I have a model field as follows with a timezone aware auto add:

start_date = models.DateTimeField(auto_now_add=True, blank=True)

However I still get a RuntimeWarning when running tests:

RuntimeWarning: DateTimeField Survey.start_date received a naive datetime (2020-07-16 03:15:14.463640) while time zone support is active.

I get the same when I have:

start_date = models.DateTimeField(default=timezone.now, blank=True)

Any thoughts?

2 Answers

This also can be caused by any case where you have custom model manager filtering any date fields on datetime.today() rather than timezone.now().

Your second option should be alright. But you should remove blank=True, since by providing a default would mean you could send a request without this field.

start_date = models.DateTimeField(default=timezone.now)

Make sure you have run python manage.py makemigrations and python manage.py migrate after you made the change.

Related