Runtime warning about a naive datetime when running the test command

2,424 views
Skip to first unread message

Daniel Grace

unread,
Nov 15, 2014, 9:22:18 AM11/15/14
to django...@googlegroups.com
Hi,
I get a runtime warning about a naive datetime when running the test command:
>python manage.py test flow

Creating test database for alias 'default'...
C:\landy\lib\site-packages\django\db\models\fields\__init__.py:1278: RuntimeWarn
ing: DateTimeField Flow.created received a naive datetime (2014-11-15 13:58:10.2
28817) while time zone support is active.
  RuntimeWarning)

Here is the field in the model:
class Flow(models.Model):
    ...
    created = models.DateTimeField(db_index=True, auto_now_add=True)

And in settings.py I have:
TIME_ZONE = 'Europe/London'
USE_TZ = True

I don't get these warnings when manually testing the application.  What is going on?

Thanks

Aliane Abdelouahab

unread,
Nov 15, 2014, 11:28:49 PM11/15/14
to django...@googlegroups.com
i think it is the same problem here (which has the solution)

André Castilho Manzano

unread,
Nov 16, 2014, 1:40:47 AM11/16/14
to django...@googlegroups.com

Hi Daniel,

This warning is not an error.
Its only telling you that your app is saving a naive date.
It means that you have a simple date instead a date with timezone.

You probably have timezone enable on your settings.py file.
It's recommended you leave it on, but if your app won't be used in another country (different timezone) you could disable it.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/637ac2a1-eece-4509-bdca-cc5476933893%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Grace

unread,
Nov 16, 2014, 8:18:52 AM11/16/14
to django...@googlegroups.com
I understand the problem.  Why doesn't the Django tester provide time zone aware date times?  I know if I set "USE_TZ = False" the warnings will disappear.  I was looking for another solution.  Could I conditionally set "USE_TZ = False" only when testing?

Daniel Grace

unread,
Nov 16, 2014, 10:51:17 PM11/16/14
to django...@googlegroups.com
Is this a bug in the Django tester?

Bruno Barcarol Guimarães

unread,
Nov 17, 2014, 6:33:40 AM11/17/14
to django...@googlegroups.com
Hi Daniel.

The problem is you're probably generating a naive datetime at some point in
your code. The best way to solve this is to run the tests with

$ python -W error manage.py test

That way an exception will be raised instead of a warning. From there you can
probably figure out where it is coming from.

André Castilho Manzano

unread,
Nov 17, 2014, 8:24:13 AM11/17/14
to django...@googlegroups.com
Daniel,
I remember doing a function to handle naive date, it was something like that.

def ConvertDateFormat(date):
    hour = " 00:00:00"
    NaiveDate = str(date) + hour
    NewDate = datetime.strptime(NaiveDate , '%d/%m/%Y %H:%M:%S').replace(tzinfo=utc)
    return NewDate


PS: I also noticed that you have "auto_now_add=True", so you probably using naive date when using filter, or something like that.


I hope it's from any help.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.



--


André Castilho
Celular: (11) 99587-3437
andrem...@gmail.com


Daniel Grace

unread,
Nov 17, 2014, 11:23:27 AM11/17/14
to django...@googlegroups.com
On Monday, 17 November 2014 11:33:40 UTC, Bruno Barcarol Guimarães wrote:

$ python -W error manage.py test

 
Hi Bruno,
I get another warning / error with that command:
>python -W error manage.py test flow
Traceback (most recent call last):
  File "manage.py", line 8, in <module>
    from django.core.management import execute_from_command_line
  File "C:\landy\lib\site-packages\django\core\management\__init__.py", line 8,
in <module>
    from django.apps import apps
  File "C:\landy\lib\site-packages\django\apps\__init__.py", line 1, in <module>

    from .config import AppConfig   # NOQA
  File "C:\landy\lib\site-packages\django\apps\config.py", line 5, in <module>
    from django.utils.module_loading import module_has_submodule
  File "C:\landy\lib\site-packages\django\utils\module_loading.py", line 4, in <
module>
    import imp
  File "C:\Python34\lib\imp.py", line 32, in <module>
    PendingDeprecationWarning)
PendingDeprecationWarning: the imp module is deprecated in favour of importlib;
see the module's documentation for alternative uses
Reply all
Reply to author
Forward
0 new messages