【问题标题】:Django change IntegerField to DecimalField on inherited modelDjango 在继承模型上将 IntegerField 更改为 DecimalField
【发布时间】: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


    【解决方案1】:

    您可以使用额外的抽象模型来更改层次结构中的字段:

    class Parent(models.Model):
       intfield = models.IntegerField()
    
        class Meta:
            abstract = True
    
    class DescendantType1(Parent):
        """This is an abstract model that inherits from the Parent model, with the "type 1"
        attributes.
        """
        addedfield = models.CharField(max_length=20)
    
        class Meta:
            abstract = True
    
    class DescendantType2(Parent):
        """This is an abstract model that inherits from the Parent model, with the "type 2"
        attributes.
        """
        addedfield2 = models.DecimalField(max_digits=10, decimal_places=2)
        linked = models.ForeignKey(
            "Child1",
            on_delete=models.CASCADE,
            # This is required on foreign key fields in abstract models.
            # See the "Note about foreign key related names" below.
            related_name="%(app_label)s_%(class)s_related",
            related_query_name="%(app_label)s_%(class)ss",
        )
    
        class Meta:
            abstract = True
    
    class Child1(DescendantType1):
        """A descendant with "type 1" attributes."""
    
    class Child2(DescendantType2):
        """A descendant with "type 2" attributes."""
    
    class GrandChild1(DescendantType1):
        intfield = models.DecimalField(...)
    
    class GrandChild2(DescendantType2):
        intfield = models.DecimalField(...)
        linked = models.ForeignKey(
            "GrandChild1",
            on_delete=models.CASCADE,
        )
    

    关于外键相关名称的注意事项

    具有外键的抽象模型需要为从它继承的每个具体模型使用不同的 related_name 和 related_query_name,否则 reverse 关系的名称将相同每个子类。

    为了处理这个问题,django 允许您将模板字符串与变量 app_label 和 class 一起使用,以便子模型的名称是唯一的。

    你可以找到more about this in the documentation。

    【讨论】:

    • 感谢您的回答。当我尝试使用 Child1 作为 DescendentType2 上的 ForeignKey 创建迁移时,它产生了一个错误。 `NameError 'Child1' 未定义。
    • 我把它移到子模型上,它工作了。
    • 这很奇怪。您是否从字符串中更改了"Child1"(通过删除引号)?如果模型尚不存在,它应该仍然能够引用该模型。不过我可能是错的。它也可能特定于 django 迁移。
    • 你说得对,我不小心错过了引号。如果我在抽象类上更改它,则意味着所有相关引用都被重命名。更改子模型似乎使一切都在前端工作。
    • 我对在抽象模型上使用 Child1 的好处有点困惑。如果重构相关查询,我猜您可以将现有模板用于所有子类。如果在子类上重新定义了链接,它是否需要新的模板?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多