What on earth is causing this "naive datetime" warning?

1,166 views
Skip to first unread message

Daniel Grace

unread,
Nov 19, 2014, 7:38:58 AM11/19/14
to django...@googlegroups.com
Hi,

Here is my model:
class Flow(models.Model):
    ref = models.CharField(max_length=32)
    state = models.ForeignKey(State)
    flow_type = models.ForeignKey(Type)
    created = models.DateTimeField(db_index=True, auto_now_add=True)
    modified = models.DateTimeField(db_index=True, auto_now=True)
    version = models.IntegerField()
    def __str__(self):
        return str(self.id)

Here is my test.py file:
import warnings
warnings.filterwarnings(
    'error', r"DateTimeField .* received a naive datetime",
    RuntimeWarning, r'django\.db\.models\.fields')


In settings.py:
TIME_ZONE = 'Europe/London'
USE_TZ = True

Here is what happens when I run the test script:
>python manage.py test flow
Creating test database for alias 'default'...
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\landy\lib\site-packages\django\core\management\__init__.py", line 385
, in execute_from_command_line
    utility.execute()
  File "C:\landy\lib\site-packages\django\core\management\__init__.py", line 377
, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\landy\lib\site-packages\django\core\management\commands\test.py", lin
e 50, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "C:\landy\lib\site-packages\django\core\management\base.py", line 288, in
 run_from_argv
    self.execute(*args, **options.__dict__)
  File "C:\landy\lib\site-packages\django\core\management\commands\test.py", lin
e 71, in execute
    super(Command, self).execute(*args, **options)
  File "C:\landy\lib\site-packages\django\core\management\base.py", line 338, in
 execute
    output = self.handle(*args, **options)
  File "C:\landy\lib\site-packages\django\core\management\commands\test.py", lin
e 88, in handle
    failures = test_runner.run_tests(test_labels)
  File "C:\landy\lib\site-packages\django\test\runner.py", line 147, in run_test
s
    old_config = self.setup_databases()
  File "C:\landy\lib\site-packages\django\test\runner.py", line 109, in setup_da
tabases
    return setup_databases(self.verbosity, self.interactive, **kwargs)
  File "C:\landy\lib\site-packages\django\test\runner.py", line 299, in setup_da
tabases
    serialize=connection.settings_dict.get("TEST_SERIALIZE", True),
  File "C:\landy\lib\site-packages\django\db\backends\creation.py", line 374, in
 create_test_db
    test_flush=True,
  File "C:\landy\lib\site-packages\django\core\management\__init__.py", line 115
, in call_command
    return klass.execute(*args, **defaults)
  File "C:\landy\lib\site-packages\django\core\management\base.py", line 338, in
 execute
    output = self.handle(*args, **options)
  File "C:\landy\lib\site-packages\django\core\management\commands\migrate.py",
line 160, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "C:\landy\lib\site-packages\django\db\migrations\executor.py", line 63, i
n migrate
    self.apply_migration(migration, fake=fake)
  File "C:\landy\lib\site-packages\django\db\migrations\executor.py", line 97, i
n apply_migration
    migration.apply(project_state, schema_editor)
  File "C:\landy\lib\site-packages\django\db\migrations\migration.py", line 107,
 in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, ne
w_state)
  File "C:\landy\lib\site-packages\django\db\migrations\operations\fields.py", l
ine 131, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "C:\landy\lib\site-packages\django\db\backends\schema.py", line 509, in a
lter_field
    self._alter_field(model, old_field, new_field, old_type, new_type, old_db_pa
rams, new_db_params, strict)
  File "C:\landy\lib\site-packages\django\db\backends\schema.py", line 612, in _
alter_field
    new_default = self.effective_default(new_field)
  File "C:\landy\lib\site-packages\django\db\backends\schema.py", line 183, in e
ffective_default
    default = field.get_db_prep_save(default, self.connection)
  File "C:\landy\lib\site-packages\django\db\models\fields\__init__.py", line 62
7, in get_db_prep_save
    prepared=False)
  File "C:\landy\lib\site-packages\django\db\models\fields\__init__.py", line 12
86, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\landy\lib\site-packages\django\db\models\fields\__init__.py", line 12
78, in get_prep_value
    RuntimeWarning)
RuntimeWarning: DateTimeField Flow.created received a naive datetime (2014-11-19
 12:28:38.831258) while time zone support is active.

What on earth is causing this "naive datetime" warning?

Thanks

Russell Keith-Magee

unread,
Nov 19, 2014, 9:44:53 AM11/19/14
to Django Users
Hi Daniel,

A naïve datetime is a datetime that doesn't have a timezone. If you have USE_TZ=True enabled in your settings file (which is the default for new projects), Django expects you to fully specify datetime objects with a timezone; if you don't, you get the warning you've seen, because the datetime you've provided is ambiguous. Django can guess, but there's no guarantee that the guess will be correct.

The django.utils.timezone package provides a few utilities to convert naïve datetimes to timezone-enabled datetimes. I'd also suggest looking into pytz, which provides a regularly updated database of the world's timezones.

Yours,
Russ Magee %-)

--
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/9483e04e-242a-4252-a707-f51e6763ef2c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Grace

unread,
Nov 19, 2014, 10:07:41 AM11/19/14
to django...@googlegroups.com
I have already installed pytz (2014.17) and I have set USE_TZ=True.  I don't use any naive datetimes in my application.  This error is caused by something that the Django test command is doing automatically, if you look at the traceback you will see that it is something to do with migrations.  I am using Django 1.7.

Carl Meyer

unread,
Nov 19, 2014, 10:11:27 AM11/19/14
to django...@googlegroups.com
Hi Daniel,
What is the default value for your Flow.created field?

Carl

signature.asc

Daniel Grace

unread,
Nov 19, 2014, 10:58:26 AM11/19/14
to django...@googlegroups.com
Hi Carl,
I was not specifying one, so I changed my model and did a "makemigrations" and a "syncdb" :

class Flow(models.Model):
    ref = models.CharField(max_length=32)
    state = models.ForeignKey(State)
    flow_type = models.ForeignKey(Type)
    created = models.DateTimeField(db_index=True, auto_now_add=True, default=timezone.now)
    modified = models.DateTimeField(db_index=True, auto_now=True, default=timezone.now)
    version = models.IntegerField()

But I still get the exact same warning (see my original message in this thread) !

Carl Meyer

unread,
Nov 19, 2014, 11:43:16 AM11/19/14
to django...@googlegroups.com
Hi Daniel,

On 11/19/2014 08:58 AM, Daniel Grace wrote:
> On Wednesday, 19 November 2014 15:11:27 UTC, Carl Meyer wrote:
> On 11/19/2014 08:07 AM, Daniel Grace wrote:
> > I have already installed pytz (2014.17) and I have set
> USE_TZ=True. I
> > don't use any naive datetimes in my application. This error is
> caused
> > by something that the Django test command is doing automatically,
> if you
> > look at the traceback you will see that it is something to do with
> > migrations. I am using Django 1.7.
>
> What is the default value for your Flow.created field?
>
> Hi Carl,
> I was not specifying one, so I changed my model and did a
> "makemigrations" and a "syncdb" :

"syncdb" is a deprecated alias for "migrate" - may as well start getting
used to doing "migrate" instead.

> class Flow(models.Model):
> ref = models.CharField(max_length=32)
> state = models.ForeignKey(State)
> flow_type = models.ForeignKey(Type)
> created = models.DateTimeField(db_index=True, auto_now_add=True,
> default=timezone.now)
> modified = models.DateTimeField(db_index=True, auto_now=True,
> default=timezone.now)
> version = models.IntegerField()
>
> But I still get the exact same warning (see my original message in this
> thread) !

If you have `auto_now_add=True`, then you don't actually want a default
(it won't have any effect).

But based on your traceback, it seems like at some point in the past
when you made a migration, the field might have had a default value
which was a naive datetime. Is that possible?

It might be necessary to look through your existing migrations for this
app to find the culprit.

Carl

signature.asc

Daniel Grace

unread,
Nov 19, 2014, 12:32:17 PM11/19/14
to django...@googlegroups.com

But based on your traceback, it seems like at some point in the past
when you made a migration, the field might have had a default value
which was a naive datetime. Is that possible?

It might be necessary to look through your existing migrations for this
app to find the culprit.


Yes Carl you were right, I cleared out my migrations folder except for the "__init__.py" file and the problem went away. 
Reply all
Reply to author
Forward
0 new messages