Django 1.7.1:子模型,删除对父模型的所有引用,并使子模型从祖父继承 [英] Django 1.7.1: Child model, remove all references to parent model and make child model inherit from grandpa instead
问题描述
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
代码已经在生产中运行,所以我需要做一些数据迁移。认为它会顺利运行,这是我打算做的:
- 保存每个
C
的b_ptr_id
到一个临时字段。 - 删除
C
在B
(删除b_ptr_id
的过程中)继承 -
C
继承自A
(在过程中添加a_ptr_id
) - 对于每个
C
,将存储在临时字段中的值复制到a_ptr_id
。 - 删除临时字段。
我的问题是第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:
- Save each
C
'sb_ptr_id
to a temporary field. - Remove
C
's inheritance fromB
(deletingb_ptr_id
in the process). - Make
C
inherit fromA
(adding ana_ptr_id
in the process). - For each
C
, copy the value stored in the temporary field toa_ptr_id
. - 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屋!