Adding a “through” table to django field and migrating with South?

后端 未结 3 1904
-上瘾入骨i
-上瘾入骨i 2021-01-04 01:41

Seems like this should be \"easy\" or at least documented somewhere, I just cant find it.

Lets say I have a model:

class A(models.Model):
    users =         


        
3条回答
  •  醉梦人生
    2021-01-04 01:54

    As mentioned in a comment, the first step may be simplified using db.rename_table as described here, which gives this through model:

    class AUsers(models.Model):
    user = models.ForeignKey('auth.User')
    a = models.ForeignKey('A')
    
    class Meta:
        unique_together = (('user', 'a'),)
    

    Then, create a migration with --auto (this way you'll have the names of the DB tables visible), and replace the content with:

    class Migration(SchemaMigration):
    
        def forwards(self, orm):
            db.rename_table('appname_a_user', 'appname_auser')
    
        def backwards(self, orm):
            db.rename_table('appname_auser','appname_a_user') 
    

    I just applied it in my project without issues.

提交回复
热议问题