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.
- Let's define the Category model in the categories app, as follows:
# myproject/apps/categories/models.py
from django.db import models
from django.utils.translation import gettext_lazy as _
from myproject.apps.core.model_fields import MultilingualCharField
class Category(models.Model):
title = MultilingualCharField(
_("Title"),
...