管理模型不能识别继承的字段 [英] Admin Models do not recognize inherited fields

查看:291
本文介绍了管理模型不能识别继承的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这是django.admin,django还是python的特定内容?但是如果我无法访问他们的字段,我并不真正理解抽象超类的含义:-)。我做错了什么?

I am wondering if this is something specific to django.admin, django, or even python? But I don't really understand the point in having abstract super classes if I cannot access their fields :-). Have I done something wrong?

示例:
我收到一个FieldError,并为Module指定了以下异常值:未知字段(creation_date)。如果我使用管理界面获取以下模型Module,请检查ModuleAdmin类的字段/ fieldsets / exclude属性:

Example: I get a FieldError with the following "Exception Value: Unknown field(s) (creation_date) specified for Module. Check fields/fieldsets/exclude attributes of class ModuleAdmin" if I use the admin interface to get the following model "Module":

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代码:

With the following ModelAdmin Code:

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

Your problem is related to the answer: https://stackoverflow.com/a/3594927/3006165

错误是由于date有auto_now_add = True(或auto_now = True)。
由于该值是自动的,因此它不可编辑,因此它不是

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

根据该文档: p>

According to the documentantion:


字段选项...只有在
readonly_fields中列出时才可能包含调用。

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

这篇关于管理模型不能识别继承的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆