Django 1.7.1:子模型,删除对父模型的所有引用,并使子模型从祖父继承 [英] Django 1.7.1: Child model, remove all references to parent model and make child model inherit from grandpa instead

查看:121
本文介绍了Django 1.7.1:子模型,删除对父模型的所有引用,并使子模型从祖父继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个模型,如:

  class A(models.Model):
pass
这里的#个字段

class B(A):
pass
#more fields here

class C(B):
pass
#more fields here

新要求要求我改为:

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

class B (A):
pass
#more fields here

class C(A):
pass
#more fields here

代码已经在生产中运行,所以我需要做一些数据迁移。认为它会顺利运行,这是我打算做的:


  1. 保存每个 C b_ptr_id 到一个临时字段。

  2. 删除 C B (删除 b_ptr_id 的过程中)继承

  3. C 继承自 A (在过程中添加 a_ptr_id

  4. 对于每个 C ,将存储在临时字段中的值复制到 a_ptr_id

  5. 删除临时字段。

我的问题是第3步。当我尝试从 A 中继承 C ,我收到错误 django.core.exceptions。 FieldError:类'C'中的本地字段'a_ptr'与基类'B'的类似名称的字段冲突。所以Django似乎仍然记得 C 用于继承 B 即使 b_ptr_id 不再是数据库中的 C 的列。如何让它忘记,以便我能继续前进?还是有更好的方式让孩子的模型从祖父模型中继承,而不是其父模型?

解决方案

不知道是否有一个更简单的方法来做这个但是....



尝试创建一个继承A的C'
然后创建一个创建C'每个C实例。
(删除C的所有实例)



如果要保留名称C,请更新C的架构,然后从C'实例重新填充C。



希望有帮助。 :)


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.
  2. Remove C's inheritance from B (deleting b_ptr_id in the process).
  3. Make C inherit from A (adding an a_ptr_id in the process).
  4. For each C, copy the value stored in the temporary field to 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'. 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. 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. (Delete all instance of C afterwards)

If you want to keep the name C, update schema for C and then repopulate C from the C' instances.

Hope it helps. :)

这篇关于Django 1.7.1:子模型,删除对父模型的所有引用,并使子模型从祖父继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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