views:

110

answers:

1

I have a Django application in which I want to change a field from a ForeignKey to a ManyToManyField. I want to preserve my old data. What is the simplest/best process to follow for this? If it matters, I use sqlite3 as my database back-end.

If my summary of the problem isn't clear, here is an example. Say I have two models:

class Author(models.Model):  
    author = models.CharField(max_length=100) 

class Book(models.Model):  
    author = models.ForeignKey(Author)  
    title = models.CharField(max_length=100)

Say I have a lot of data in my database. Now, I want to change the Book model as follows:

class Book(models.Model):  
    author = models.ManyToManyField(Author)  
    title = models.CharField(max_length=100) 

I don't want to "lose" all my prior data.

What is the best/simplest way to accomplish this?

Ken

+1  A: 

Check out south.

Zach
More specifically check out "data migrations" section of the tutorial: http://south.aeracode.org/wiki/Tutorial3It's a good habit to use South for all your migrations anyway.
Ludwik Trammer
Great tool, thanks for the link!
lupefiasco