gpt4 book ai didi

django - 管理模型无法识别继承的字段

转载 作者:行者123 更新时间:2023-12-04 05:30:57 27 4
gpt4 key购买 nike

我想知道这是否是django.admin,django甚至python特有的东西吗?但是,如果我不能访问它们的字段,我真的不理解拥有抽象父类(super class)的意义:-)。我做错什么了吗?

例子:
如果使用管理界面获取以下模型“Module”,则收到带有以下“异常值:为Module指定的未知字段(creation_date)的FieldError。请检查类ModuleAdmin的field/fieldsets/exclude属性”。

class GeneralModel(models.Model):
creation_date = models.DateTimeField('date of creation', auto_now_add=True)
edited_date = models.DateTimeField('date of last modification', auto_now=True)

class Meta:
abstract = True


class Module(GeneralModel):
name = models.CharField(max_length=100)
shortDescription = models.CharField("summary", max_length=100)
description = models.CharField("description", max_length=1500)
authors = models.ManyToManyField("Author", through="Authorship")

def __unicode__(self):
return self.name

使用以下ModelAdmin代码:
class ModuleAdmin(admin.ModelAdmin):
def formfield_for_dbfield(self, db_field, **kwargs):
formfield = super(ModuleAdmin, self).formfield_for_dbfield(db_field, **kwargs)
if db_field.name == 'description':
formfield.widget = forms.Textarea(attrs=formfield.widget.attrs)
return formfield
fieldsets = [
("General", {"fields": ["name", "shortDescription"]}),
("Details", {"fields": ["description", "creation_date"], "classes": ["collapse"]})
]

最佳答案

您的问题与答案有关:
https://stackoverflow.com/a/3594927/3006165

The error is due to date having auto_now_add=True (or auto_now=True). As the value is automatic, it's not editable, so it's not in the form



根据文献:

The fields option ... may contain callables only if they are listed in readonly_fields.

关于django - 管理模型无法识别继承的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22682516/

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