Search code examples
pythondjangodjango-southdjango-timezone

Default value of DateTimeField for South migration in Django project with activated timezone support


I'm creating a schema migration with South 0.7.6 for my Django 1.4.3 project with enabled timezone support.

The schema migration includes adding a DateTimeField (with auto_now=True) on one table.

When creating the migration, South prompts me:

The field 'MyTable.my_field' does not have a default specified, yet is NOT NULL.
Since you are adding this field, you MUST specify a default
value to use for existing rows. Would you like to:
 1. Quit now, and add a default to the field in models.py
 2. Specify a one-off value to use for existing columns now

What's the correct one-off value to give here, if I don't care about this value for existing rows (I just want the migration to succeed without warnings)?

So far, I used datetime.datetime.utcnow(). However, then I get the following when applying the migration:

C:\Python27\lib\site-packages\django\db\models\fields\__init__.py:808:
RuntimeWarning: DateTimeField received a naive datetime (2013-01-16 00:00:00)
while time zone support is active.

South does not seem to import pytz or the Django helper classes, so how can I give a timezone-aware default value here?


Solution

  • Manually edit the migration file that South created and add:

    from django.utils import timezone
    

    Then find the field that you are adding in the migration file and set its default to timezone.now().