gpt4 book ai didi

python - Django: _meta.get_fields() : include_parents=False : 显示继承自的字段

转载 作者:行者123 更新时间:2023-12-05 06:17:01 25 4
gpt4 key购买 nike

我有以下模型:

class CommonModel(models.Model):
"""
Default Common Variables In Database
"""

class Meta:
abstract = True

created_by = models.ForeignKey(User,
related_name="%(app_label)s_%(class)s_created_by",
null=True, blank=True,on_delete=models.SET(get_sentinel_user))
modified_by = models.ForeignKey(User,
related_name="%(app_label)s_%(class)s_modified_by",
null=True, blank=True,on_delete=models.SET(get_sentinel_user))
deleted_by = models.ForeignKey(User,
related_name="%(app_label)s_%(class)s_deleted_by",
null=True, blank=True,on_delete=models.SET(get_sentinel_user))
is_deleted = models.BooleanField(default=False)
created_date = models.DateTimeField(blank=True)
deleted_date = models.DateTimeField(null=True, blank=True)
modified_date = models.DateTimeField(auto_now=True)

class Article(CommonModel):
title = models.CharField(max_length=256)
intro_image = models.ImageField(null=True, blank=True, upload_to=doc_hash)
description = models.TextField(null=True, blank=True)
url = models.CharField(max_length=256, null=True, blank=True)
slug = models.SlugField(editable=False)

我正在 ipython 中尝试以下操作

> Article._meta.get_fields()

(<django.db.models.fields.AutoField: id>,
<django.db.models.fields.related.ForeignKey: created_by>,
<django.db.models.fields.related.ForeignKey: modified_by>,
<django.db.models.fields.related.ForeignKey: deleted_by>,
<django.db.models.fields.BooleanField: is_deleted>,
<django.db.models.fields.DateTimeField: created_date>,
<django.db.models.fields.DateTimeField: deleted_date>,
<django.db.models.fields.DateTimeField: modified_date>,
<django.db.models.fields.CharField: title>,
<django.db.models.fields.files.ImageField: intro_image>,
<django.db.models.fields.TextField: description>,
<django.db.models.fields.CharField: url>,
<django.db.models.fields.SlugField: slug>

我尝试使用 include_parents=False

> Article._meta.get_fields(include_parents=False)

(<django.db.models.fields.AutoField: id>,
<django.db.models.fields.related.ForeignKey: created_by>,
<django.db.models.fields.related.ForeignKey: modified_by>,
<django.db.models.fields.related.ForeignKey: deleted_by>,
<django.db.models.fields.BooleanField: is_deleted>,
<django.db.models.fields.DateTimeField: created_date>,
<django.db.models.fields.DateTimeField: deleted_date>,
<django.db.models.fields.DateTimeField: modified_date>,
<django.db.models.fields.CharField: title>,
<django.db.models.fields.files.ImageField: intro_image>,
<django.db.models.fields.TextField: description>,
<django.db.models.fields.CharField: url>,
<django.db.models.fields.SlugField: slug>

它显示相同的结果。

我认为它不应该显示从 CommonModel

继承的字段

可以解释一下 include_parents=False 在这种情况下会做什么。

最佳答案

As doc says

  • include_parents:

    True by default. Recursively includes fields defined on parent classes. If set to False, get_fields() will only search for fields declared directly on the current model. Fields from models that directly inherit from abstract models or proxy classes are considered to be local, not on the parent.

在您的例子中,CommonModel 声明为 abstract 模型。因此,CommonModel 中的所有字段都被视为本地字段。我认为这就是为什么你有相同的结果。

关于python - Django: _meta.get_fields() : include_parents=False : 显示继承自的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61838149/

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