gpt4 book ai didi

python - 如何覆盖父类中的字段

转载 作者:太空狗 更新时间:2023-10-29 18:26:51 27 4
gpt4 key购买 nike

我在 Django 模型中有父类和子类。我想在初始化子类时在父类中填充一个字段。或者在子类中覆盖此字段。

    class Parent(models.Model):
type = models.CharField()

class Child(Parent):
type = models.CharField() //Doesn't work

也尝试覆盖 init 方法,但它也不起作用。我怎样才能做到这一点?

最佳答案

In normal Python class inheritance, it is permissible for a child class to override any attribute from the parent class. In Django, this is not permitted for attributes that are Field instances (at least, not at the moment). If a base class has a field called author, you cannot create another model field called author in any class that inherits from that base class.

你不能。 Reference

关于python - 如何覆盖父类中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6897730/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com