This recipe is a practical example of how to change a many-to-one relation to a many-to-many relation, while preserving the already existing data. We will use both schema and data migrations in this situation.
Changing a foreign key to the many-to-many field
Getting ready
Let's suppose that you have the Idea model, with a foreign key pointing to the Category model, as follows:
# demo_app/models.py
from django.db import models
from django.utils.translation import ugettext_lazy as _
class Category(models.Model):
title = models.CharField(_("Title"), max_length=200)
def __str__(self):
return self.title
class Idea(models.Model):
title = model.CharField(
_("Title"),
...