Where is this warning coming from?

46 views
Skip to first unread message

jorr...@gmail.com

unread,
Jun 9, 2016, 7:43:33 AM6/9/16
to Django users
I have a view that calculates some quick statistics:

def index(request):
context = {
'topics_unanswered': Topic.on_site.filter(topic_type=1, date_published__isnull=False, reply__isnull=True).count(),
'members_unapproved': User.objects.filter(profile__is_approved=False).count(),
'members_unverified_7days': User.objects.filter(profile__is_approved=False, date_joined__lte=timezone.now()-timezone.timedelta(days=7), profile__email_verified=False).count(),
'topics_today': Topic.on_site.filter(date_published__gte=timezone.now().date()).count(),
'replies_today': Reply.on_site.filter(date_published__gte=timezone.now().date()).count(),
'members_today': User.objects.filter(date_joined__gte=timezone.now().date()).count(),
}
return render(request, 'admincp/index.html', context)

But when I open the page I get the following warnings:

...\lib\site-packages\django\db\models\fields\__init__.
py
:1393: RuntimeWarning: DateTimeField Topic.date_published received a naive dat
etime
(2016-06-09 00:00:00) while time zone support is active.
 
RuntimeWarning)

...\lib\site-packages\django\db\models\fields\__init__.
py
:1393: RuntimeWarning: DateTimeField Reply.date_published received a naive dat
etime
(2016-06-09 00:00:00) while time zone support is active.
 
RuntimeWarning)

...\lib\site-packages\django\db\models\fields\__init__.
py
:1393: RuntimeWarning: DateTimeField User.date_joined received a naive datetim
e
(2016-06-09 00:00:00) while time zone support is active.
 
RuntimeWarning)

[09/Jun/2016 13:38:09] "GET /admincp/ HTTP/1.1" 200 14002

I'm using timezone(), so why am I getting these warnings?

Thanks for the help!

James Schneider

unread,
Jun 9, 2016, 10:48:20 AM6/9/16
to django...@googlegroups.com

> py:1393: RuntimeWarning: DateTimeField User.date_joined received a naive datetim
> e (2016-06-09 00:00:00) while time zone support is active.
>   RuntimeWarning)
>
> [09/Jun/2016 13:38:09] "GET /admincp/ HTTP/1.1" 200 14002
>
> I'm using timezone(), so why am I getting these warnings?
>

What is USE_TZ set to? If it is set to False (the default), you'll get naive datetime values from django.utils.timezone:

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

You can use timezone.is_aware() to verify.

https://docs.djangoproject.com/en/1.9/ref/utils/#django.utils.timezone.is_aware

-James

Tim Graham

unread,
Jun 9, 2016, 12:59:44 PM6/9/16
to Django users
timezone.now().date() returns datetime.date() which doesn't have a timezone.

You can use the date lookup in Django 1.9+: https://docs.djangoproject.com/en/stable/ref/models/querysets/#date

jorr...@gmail.com

unread,
Jun 12, 2016, 1:16:53 PM6/12/16
to Django users
I have USE_TZ set to True.

Thanks Tim, I will try using the date lookup!
Reply all
Reply to author
Forward
0 new messages