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