添加“直通”表到django字段和南移? [英] Adding a "through" table to django field and migrating with South?

查看:126
本文介绍了添加“直通”表到django字段和南移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎这样应该是容易的,或者至少记录在某个地方,我只是找不到它。



说我有一个模型:

  class A(models.Model):
users = models.ManyToMany('auth.User',blank = True)

现在我想迁移到添加到ManyToMany关系的字段...

  class AUsers(models.Model):
user = models.ForeignKey ('auth.User')
a = models.ForeignKey('A')
new_field = models.BooleanField()

class A(models.Model):
users = models.ManyToMany('auth.User',blank = True,through ='AUsers')

然后我做:

 %./manage.py schemamigration app --auto 

不完全令人惊讶,它告诉我将要通过表删除原始的自动创建,并为 AUsers 。这时最好的做法是什么?有没有一种体面的方法来迁移到新的通过表?我在Meta中使用 db_table 吗?我不会立即使用 through = ... 然后执行 schemamigration --auto 然后一个 datamigration 复制当前表(不知何故,不确定...),然后通过关系添加让它杀死桌子?



这里有什么招数?这真的很难吗?

解决方案

你应该可以很容易地做到这一点。



首先,确保您正在创建的手册通过表具有与Django自动创建的数据库中相同的表名。



所以,首先,在更改之前,先考虑一个手册:

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

class Meta:
db_table ='appname_a_user'$ b $这应该是功能上(几乎)与 ManyToManyField 相同的



<你以前有过实际上,您可以进行空迁并应用,然后使用--auto进行更改(但不要)。



现在,像上面的示例代码一样添加您的字段,然后运行 ./ manage.py schemamigration appname manual_through_table --empty 。这将给你一个名为 #### _ manual_through_table.py 的空移。



在迁移本身,那里将是转发向后方法。每个需要一行:

  def forward(self,orm):
db.add_column(' appname_a_user','new_field',self.gf('django.db.models.fields.BooleanField')(default = False))

def backwards(self,orm):
db .delete_column('appname_a_user','new_field')

/ p>

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 = models.ManyToMany('auth.User', blank=True)

Now I want to migrate to have a through table to add fields to the ManyToMany relation...

class AUsers(models.Model):
    user = models.ForeignKey('auth.User')
    a = models.ForeignKey('A')
    new_field = models.BooleanField()

class A(models.Model):
    users = models.ManyToMany('auth.User', blank=True, through='AUsers')

Then I do:

% ./manage.py schemamigration app --auto

Not totally surprising, it tells me it is going to drop the original auto-created through table and create a new one for AUsers. What's the best practice at this point? Is there a decent way to migrate to the new through table? Do I use db_table in Meta? Do I just not use the through=... right away... then do a schemamigration --auto, then a datamigration to copy the current table (somehow, not sure...) and then add the through relation and let it kill the table?

What's the trick here? Is this really that hard?

解决方案

You should be able to do this pretty easily.

First of all, make sure that the manual through table that you are creating has the same table name in the database as the one Django originally created automatically.

So, first, let's consider a manual through model before your change:

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

    class Meta:
        db_table = 'appname_a_user'

That should be functionally (almost) identical to the ManyToManyField you used to have. Actually, you could make an empty migration and apply it, and then use --auto for your changes (but don't).

Now, add your field like you did in your sample code above, and then run ./manage.py schemamigration appname manual_through_table --empty. That will give you an empty migration named ####_manual_through_table.py.

In the migration itself, there will be a forwards and backwards method. Each one needs to be one line each:

def forwards(self, orm):
    db.add_column('appname_a_user', 'new_field', self.gf('django.db.models.fields.BooleanField')(default=False))

def backwards(self, orm):
    db.delete_column('appname_a_user', 'new_field')

That should get you what you are after.

这篇关于添加“直通”表到django字段和南移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆