Migrating data from “Many-To-Many” to “Many-To-Many Through” in django

前端 未结 4 1808
被撕碎了的回忆
被撕碎了的回忆 2020-12-29 06:47

I\'ve got a model

class Category(models.Model):
    title           = models.CharField(...)
    entry           = models.ManyToManyField(Entry,null=True,blan         


        
4条回答
  •  感情败类
    2020-12-29 07:19

    I'd do it in the following way:

    1. Add the CategoryEntry class to the model, and do an auto schema migration. This will add an empty table containing the properties of CategoryEntry. To be noted, the older M2M table remains untouched since through='CategoryEntry' has not yet been added.

    2. Do a data migration to copy all data from the existing M2M table to the table created in step 1. To do so, run the datamigration command, and edit methods forward() and backward() in the auto generated migration script accordingly.

    3. Now add through='CategoryEntry' part (just the way you wanted), and do a schemamigration. this will drop the old M2M table.

提交回复
热议问题