【发布时间】:2021-06-03 16:33:55
【问题描述】:
在 Django 中,我有 2 个从抽象基类继承的模型。基类有一个 IntegerField,我想为某些实例重命名并更改为 DecimalField。有一个 ForeignKey 链接 2 个子模型。我在文档中阅读了有关模型继承的信息,但似乎有一些 constraints。将 IntegerField 更改为 DecimalField 的最佳方法是什么?
class Parent(models.Model):
intfield = models.IntegerField()
class Meta:
abstract=True
class Child1(Parent):
addedfield = models.CharField(max_length=20)
class Child2(Parent):
addedfield2 = models.DecimalField(max_digits=10, decimal_places=2)
linked = models.ForeignKey(Child1, on_delete=models.CASCADE)
class GrandChild1(Child1):
# change intfield to decimal
class GrandChild2(Child2):
# change intfield to decimal
# linked to GrandChild1 instead of Child1
【问题讨论】:
标签: django inheritance django-models