Django data migration when changing a field to ManyToMany

Posted by Ken H on Stack Overflow See other posts from Stack Overflow or by Ken H
Published on 2010-02-08T19:50:23Z Indexed on 2010/03/29 2:33 UTC
Read the original article Hit count: 632

Filed under:
|

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

© Stack Overflow or respective owner

Related posts about django

Related posts about django-models