简体   繁体   English

Django 1.7.1:子模型,删除对父模型的所有引用,并使子模型从爷爷继承

[英]Django 1.7.1: Child model, remove all references to parent model and make child model inherit from grandpa instead

I have three models like: 我有三种模型:

class A(models.Model):
    pass
    # more fields here

class B(A):
    pass
    # more fields here

class C(B):
    pass
    # more fields here

New requirements require me to instead have: 新要求要求我改为:

class A(models.Model):
    pass
    # more fields here

class B(A):
    pass
    # more fields here

class C(A):
    pass
    # more fields here

The code is already running in production, so I need to do some data migration. 该代码已经在生产中运行,因此我需要进行一些数据迁移。 Thinking that it would work smoothly, here's what I planned to do: 认为它可以顺利运行,这是我计划要做的事情:

  1. Save each C 's b_ptr_id to a temporary field. 将每个Cb_ptr_id保存到一个临时字段。
  2. Remove C 's inheritance from B (deleting b_ptr_id in the process). B删除C的继承(在进程中删除b_ptr_id )。
  3. Make C inherit from A (adding an a_ptr_id in the process). 使CA继承(在进程中添加a_ptr_id )。
  4. For each C , copy the value stored in the temporary field to a_ptr_id . 对于每个C ,将存储在临时字段中的值复制到a_ptr_id
  5. Delete the temporary field. 删除临时字段。

My problem is with step 3. When I try to make inherit C from A , I'm getting the error django.core.exceptions.FieldError: Local field 'a_ptr' in class 'C' clashes with field of similar name from base class 'B' . 我的问题出在步骤3上。当我尝试从A继承C时,出现错误django.core.exceptions.FieldError: Local field 'a_ptr' in class 'C' clashes with field of similar name from base class 'B' So it seems like Django still "remembers" that C used to inherit from B , even if b_ptr_id is no longer a column of C in the database . 因此, 即使b_ptr_id不再是数据库中C的列, Django似乎仍然“记住” C曾经从B继承。 How do I make it forget so that I can move on? 我如何忘记它以便继续前进? Or is there a better way to make child models inherit from its grandparent model instead of its parent model? 还是有更好的方法使子模型继承自其祖父母模型而不是其父模型?

Not sure if there's an easier way to do this but.... 不知道是否有更简单的方法来做到这一点,但是...

Try creating a C' that inherits A. Then create a program that creates a C' instance of each C instance. 尝试创建继承A的C'。然后创建一个程序,该程序创建每个C实例的C'实例。 (Delete all instance of C afterwards) (此后删除C的所有实例)

If you want to keep the name C, update schema for C and then repopulate C from the C' instances. 如果要保留名称C,请更新C的架构,然后从C'实例重新填充C。

Hope it helps. 希望能帮助到你。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM