从“多对多”迁移数据到“多对多通过”在django [英] Migrating data from "Many-To-Many" to "Many-To-Many Through" in django

查看:180
本文介绍了从“多对多”迁移数据到“多对多通过”在django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型

class Category(models.Model):
    title           = models.CharField(...)
    entry           = models.ManyToManyField(Entry,null=True,blank=True,
                                             related_name='category_entries',
                                             )

我想重构每个关系的附加数据:

That I wish to refactor to have additional data with each relationship:

class Category(models.Model):
    title           = models.CharField(...)
    entry           = models.ManyToManyField(Entry,null=True,blank=True,
                                             related_name='category_entries',
                                             through='CategoryEntry',
                                             )



<但是南方删除现有的表。如何保留现有的mtm关系?

But south deletes the existing table. How can I preserve the existing m-t-m relationships?

推荐答案


  1. 额外的领域,现在。给它一个唯一的约束,以匹配现有的约束,并指定表名称以匹配现有的名称:

  1. Create your intermediate model without any extra fields, for now. Give it a unique constraint to match the existing one and specify the table name to match the existing one:

class CategoryEntry(models.Model):
    category = models.ForeignKey(Category)
    entry = models.ForeignKey(Entry)   

    class Meta:
        db_table='main_category_entries'   #change main_ to your application
        unique_together = (('category', 'entry'))


  • 运行南方模式迁移

  • Run the South schema migration.

    编辑生成的模式迁移脚本,并注释掉所有的向前和向后的条目,使用现有的交叉表。添加 pass 来完成这些方法。

    Edit the generated schema migration script and comment-out all the forwards and backwards entries, since you'll be re-using the existing intersection table. Add pass to complete the methods.

    运行迁移。

    更新任何现有的代码。如 https:// docs中所述。 djangoproject.com/en/dev/topics/db/models/#many-to-many-relationships ,与正常的多对多字段不同,您不能使用添加,创建或分配来创建关系,所以你需要修改任何现有的应用程序代码,例如

    Update any existing code. As it says in https://docs.djangoproject.com/en/dev/topics/db/models/#many-to-many-relationships, "Unlike normal many-to-many fields, you can't use add, create, or assignment to create relationships" so you'll need to modify any existing application code, e.g.

    c.entry.add(e)
    

    可能成为:

    try:
        categoryentry = c.categoryentry_set.get(entry = e)
    except CategoryEntry.DoesNotExist:
        categoryentry = CategoryEntry(category=c, entry=e)
        categoryentry.save()
    

    和:

    e.category_entries.add(c)
    

    可能成为:

    categoryentry = CategoryEntry(category=c, entry=e)  #set extra fields here
    categoryentry.save()                
    

    和:

    c.entry.remove(e)
    

    可能成为:

    categoryentry = c.categoryentry_set.get(entry = e)
    categoryentry.delete()
    


  • 一旦这个初始伪迁移完成,您应该可以将额外的字段添加到 CategoryEntry 并正常创建进一步的迁移。

  • Once this initial pseudo migration has been done, you should then be able to add the extra fields to the CategoryEntry and create further migrations as normal.

    这篇关于从“多对多”迁移数据到“多对多通过”在django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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