How to migrate from the south when using "through" for the ManyToMany field?

I want to change the ManyToMany field in a django application.

I have the following models:

class A:
    ...

class B:
    as = models.ManyToManyField(A)

I want too:

class A:
    ...

class C:
     a = models.ForeignKey(A)
     b = models.ForeignKey("B")
     extra_data = models.BooleanField(default=False)

class B:
    as = models.ManyToManyField(A, through=C)

I use south to migrate db. Unfortunately, in this case, south proxy to delete the existing table app_b_aand recreate the new one app_c.

Is there a way to tell sub not to recreate the m2m table?

+5
source share
3 answers

I would try to do it as follows:

  • C, . 2 FK , m2m , .
  • schemamigration --auto .
  • "" "" , , forwards/backwards : : db.rename_table('yourappname_m2mtablename', 'yourappname_c') : db.rename_table('yourappname_c', 'yourappname_m2mtablename')
  • "" dict !
  • C .
+6

. .

, through ManyToManyField. , .

$ python manage.py schemamigration --auto yourapp

-, datamigration:

$ python manage.py datamigration yourapp name_of_migration

:

for b in orm.B.objects.all():
    for a in b.as.all():
        orm.C.objects.create(a=a, b=b)

:

for c in orm.C.objects.all():
    c.b.as.add(a)

, through ManyToManyField :

$ python manage.py schemamigration --auto yourapp
+2

You can also specify the db_table parameter for C as 'app_b_a'. But not sure if this will work.

0
source

All Articles