Search code examples
djangointegerdjango-southdatabase-migration

South migrate DateField to IntegerField


I want to change my Model

class Source(models.Model):
    release_date = models.DateField()

to

class Source(models.Model):
    release_date = models.IntegerField()

as expected, I get an error

django.db.utils.DataError: (1264, "Out of range value for column 'release_date' at row 1")

What I actually want is, to only save the year in the IntegerField as that is all I need. Is there a very intelligent way to take existing dates' year field and migrate it to the new IntegerField by altering the method

def forwards(self, orm):

If not, how at all can I change the fieldtype without loosing all data, loosing the stored dates, would be a price i'd pay.


Solution

  • One option is to split the task into 3 migrations:

    • schema migration: adding release_year field
    • data migration: filling release_year field from release_date field
    • schema migration: remove release_date field and rename release_year to release_date

    Schema migrations would be caught by south automagically (using schemamigration --auto), but you need to write a data migration manually, here's how your forwards() method should look like:

    def forwards(self, orm):
        for source in orm.Source.objects.all():
            source.release_year = source.release_date.year
            source.save()
    

    Hope that helps.