gpt4 book ai didi

django - 为什么 Abstract=True 不在 django 模型的 Meta 类中继承

转载 作者:行者123 更新时间:2023-12-05 08:31:03 24 4
gpt4 key购买 nike

我在 django 中有这个模型:

class FotherModel(models.Model):
# Some fields goes here!
class Meta:
# Some fields goes here!
abstract = True
class ChildModel(FotherModel):
# Some fields goes here!
class Meta(FotherModel.Meta):
#s Some fields goes here!

当我们从 Django 模型的元类继承一个字段时,该字段出现在子元类中,但此规则不适用于 abstract=True

我知道如果发生这种情况,将不会在数据库中创建任何表,但我不知道这种继承怎么没有发生。请为我解释一下这个过程。

最佳答案

模型元类重置模型元类中的abstract。在 this你可以看到的文档:

Django does make one adjustment to the Meta class of an abstract base class: before installing the Meta attribute, it sets abstract=False. This means that children of abstract base classes don’t automatically become abstract classes themselves.

此外,您可以在这个link 中看到这个过程的源代码。 :

if abstract:
# Abstract base models can't be instantiated and don't appear in
# the list of models for an app. We do the final setup for them a
# little differently from normal models.
attr_meta.abstract = False
new_class.Meta = attr_meta
return new_class

关于django - 为什么 Abstract=True 不在 django 模型的 Meta 类中继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60238246/

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