ManyToManyField和South迁移

时间:2011-05-23 07:29:00

标签: django django-models django-south

我有M2M字段的用户个人资料模型

class Account(models.Model):
    ...
    friends = models.ManyToManyField('self', symmetrical=True, blank=True)
    ...

现在我需要知道如何以及何时作为朋友互相添加 我为那个

创建了一个模型
class Account(models.Model):
    ...
    friends = models.ManyToManyField('self', symmetrical=False, blank=True, through="Relationship")
    ...


class Relationship(models.Model):    
    """ Friends """        
    from_account = models.ForeignKey(Account, related_name="relationship_set_from_account")            
    to_account = models.ForeignKey(Account, related_name="relationship_set_to_account")
    # ... some special fields for friends relationship

    class Meta:                    
        db_table = "accounts_account_friends"            
        unique_together = ('from_account','to_account')

我是否应该为此更改创建任何迁移? 如果您有任何建议,请随时写下来。

由于

PS:accounts_account表已包含记录

2 个答案:

答案 0 :(得分:8)

首先,如果可以,我会避免使用db_table别名。这使得理解表结构变得更加困难,因为它不再与模型同步。

其次,South API提供db.rename_table()等功能,可以通过手动编辑迁移文件来使用。您可以将accounts_account_friends表重命名为accounts_relation(因为Django默认将其命名),并添加其他列。

这个组合为您提供了以下迁移:

def forwards(self, orm):
    # the Account.friends field is a many-to-many field which got a through= option now.
    # Instead of dropping+creating the table (or aliasing in Django),
    # rename it, and add the required columns.

    # Rename table
    db.delete_unique('accounts_account_friends', ['from_account', 'to_account'])
    db.rename_table('accounts_account_friends', 'accounts_relationship')

    # Add extra fields
    db.add_column('accounts_relationship', 'some_field',  ...)

    # Restore unique constraint
    db.create_unique('accounts_relationship', ['from_account', 'to_account'])


def backwards(self, orm):

    # Delete columns
    db.delete_column('accounts_relationship', 'some_field')
    db.delete_unique('accounts_relationship', ['from_account', 'to_account'])

    # Rename table
    db.rename_table('accounts_relationship', 'accounts_account_friends')
    db.create_unique('accounts_account_friends', ['from_account', 'to_account'])


models = {
    # Copy this from the final-migration.py file, see below
}

删除并重新创建唯一关系,以使约束具有正确的名称。

使用以下技巧可以轻松生成添加列语句:

  • 仅在Relationship中使用外键字段添加models.py模型,并且尚未对M2M字段进行任何更改。
  • 迁移到它
  • 将字段添加到Relationship模型。
  • 执行./manage.py schemamigration app --auto --stdout | tee final-migration.py | grep column
  • 还原第一次迁移。

然后,您拥有构建迁移文件所需的一切。

答案 1 :(得分:1)

你在那里编写代码的方式,你手动定义一个模型,它与Django自动为你创建的m2m连接表完成相同的工作。问题是,自动创建的表将被称为accounts_relationship_friend

那么,你在那里做的将会创建一个模型,试图复制ORM在表面下所做的事情,但是它指的是错误的表格。

如果您不需要显式连接模型,我会将其从代码库中删除,而不是创建迁移来添加它,而是使用M2M查找朋友之间的关系。 (我没有深入思考这个问题,但它应该有效)。

但是,如果你想对你所拥有的关系模型做一些特别的事情(例如存储关于关系类型的属性等),我会将关系模型声明为你在Friend.friends中使用的直通模型m2m定义。 See the docs here.

相关问题