2

I have a django project with database migrations.
MyModel.my_field represents a DateTimeField on a model MyModel

The initial migration had a naive datetime as default value (i.e. datetime.datetime.now)
A new migration now has it as django.utils.timezone.now
I don't load any fixtures as part of migration

Whenever i test the project I get this warning.

.../django/db/models/fields/__init__.py:1474: RuntimeWarning: DateTimeField MyModel.my_field received a naive datetime (2015-05-26 05:10:33) while time zone support is active.

At first I thought I was crazy and couldn't find where i was setting a naive datetime. But I ran a dummy test (i.e. a testcase with nothing to do with MyModel that did nothing) and i still got the warning.

This lead me to believe that the issue was because of the initial migration when django creates the database. I attempted to squash the migrations and rerun the tests but I still get the same warning.

Where should I be looking?
I've tried turning warnings into exceptions as indicated in the docs but that just seemed to indicate it happened while creating database.

import warnings
warnings.filterwarnings(
        'error', r"DateTimeField .* received a naive datetime",
        RuntimeWarning, r'django\.db\.models\.fields')

relevant part of initial migration:

migrations.CreateModel(
    name='MyModel',
    fields=[        
        ('my_field', models.DateTimeField(default=datetime.datetime.now)),        
    ],
    options={
    },
    bases=(models.Model,),
),

migration where we change the default value

class Migration(migrations.Migration):

    dependencies = [
        ('myapp', '0003_auto_20150101_2018'),
    ]

    operations = [
        migrations.AlterField(
            model_name='mymodel',
            name='my_field',
            field=models.DateTimeField(default=django.utils.timezone.now),
        ),
    ]

EDIT

Following @shang-wang suggestion below I ran the test with --verbosity 2. I was able to see the specific migration causing the issue. Basically between the initial and last migration the DateTimeField got the auto_now_add attribute set to True. I thought this was odd and that a squashed migration should've fixed this.

I squashed the migrations again and it is no longer throwing the warning. I have no explanation for why I thought i was seeing the RuntimeWarning the first time i created the squashed migratons

w--
  • 6,427
  • 12
  • 54
  • 92
  • Can you do `./manage.py test --verbosity 2` and see if django would begin to apply migrations when the test starts? – Shang Wang Sep 09 '15 at 18:59
  • what a great suggestion! I'm now able to see the specific migration causing the issue. pls see my edit for update. – w-- Sep 10 '15 at 06:40
  • if you want you can leave an answer and i'll accept it. Thanks – w-- Sep 10 '15 at 06:55

1 Answers1

2

Just to bring what has been discussed here, when django is running tests, it will apply the migrations and test them. However it might not show what migrations it is applying, so it's not obvious to find the place where warning occurs. To make the output verbose, do:

./manage.py test --verbosity 2

Then the migration that cause the issue can be found by looking at the verbose output.

If you are using South for the migrations, disabling the migrations when running test is easy, here's an answer about it. I'm not sure how to disable django built in migration when testing, but you might find it useful here.

Community
  • 1
  • 1
Shang Wang
  • 24,909
  • 20
  • 73
  • 94