Search code examples
djangodjango-south

Changing DateField to DateTimeField in Django


In my current models file for my app I've got some publish date fields, start and end date. However I'd like to change them from DateFields to DateTimeFields. This will give the user a little more flexibility when setting a publish date, as they would like to specify a time.

I'm using south to do the database migrations, will I have any problems when changing the field in the database? Or will need to remove it? Then add it as a DateTimeField?

Once I've done that, will i be able to set a default time? for example I'd like the start date to start at 9 in the morning. and the end date to finish at midnight.

Anyway here are the models i'd like to change.

start_date = models.DateField()
end_date = models.DateField()

I'd like to change to:

start_date = models.DateTimeField()
end_date = models.DateTimeField()

Any help would be appreciated.


Solution

  • It turns out when you change a DateField to a DateTimeField south will just change it for you so it won't conflict.

    For example I ran:

    python manage.py schemamigration --auto hotel
    

    and got this output:

     ~ Changed field start_date on hotel.Event
     ~ Changed field end_date on hotel.Event
    Created 0018_auto__chg_field_event_start_date__chg_field_event_end_date.py. You can now apply this migration with: ./manage.py migrate hotel
    

    Although i would advide backing up the database before making any changes.