FieldError:为事件指定的未知字段(received_time)。检查 EventAdmin 类的字段/字段集/排除属性

cod*_*321 2 django django-models django-admin

我有以下型号

class Event(models.Model):
    product_type = models.CharField(max_length=250, null=False, blank=False)
    received_time = models.DateTimeField(editable=False)
Run Code Online (Sandbox Code Playgroud)

在管理员中:

class EventAdmin(admin.ModelAdmin):
    fields = ['product_type', 'received_time']
Run Code Online (Sandbox Code Playgroud)

尝试编辑事件时出现以下错误(单击管理中的单个事件):

FieldError at /admin/events/event/20/
Unknown field(s) (received_time) specified for Event. Check fields/fieldsets/exclude attributes of class EventAdmin.
Run Code Online (Sandbox Code Playgroud)

我确实看到了,editable=False但我仍然希望它至少是可见的,即使它不可编辑。有没有办法修复这个错误并在管理员中编辑这些项目?

San*_*pal 7

你需要把它保存在 readonly_fields

class EventAdmin(admin.ModelAdmin):
    fields = ['product_type',]
    readonly_fields=('received_time',)
Run Code Online (Sandbox Code Playgroud)