Django ModelAdmin - 字段集...表单中缺少字段“日期" [英] Django ModelAdmin - fieldsets ... field 'date' missing from the form
本文介绍了Django ModelAdmin - 字段集...表单中缺少字段“日期"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在写这个问题时发现了问题所在.无论如何我都会发布它以防它可以帮助其他人.
错误:FooAdmin.fieldsets[0][1]['fields']"指的是表单中缺少的字段日期".
The error: 'FooAdmin.fieldsets[0][1]['fields']' refers to field 'date' that is missing from the form.
使用以下代码:
# models.py
from django.db import models
class Foo(Base):
date = models.DateField(auto_now_add=True)
title = models.CharField(max_length=255)
# admin.py
from django.contrib import admin
class FooAdmin(BaseAdmin):
list_display = ("title", "date")
fieldsets = (
(None, {
"fields": ("date", "title")
}),
)
admin.site.register(Foo, FooAdmin)
推荐答案
错误是由于日期有 auto_now_add=True
(或 auto_now=True
).
由于该值是自动的,因此不可编辑,因此不在表单中.要解决这个问题,请在 FooAdmin
中添加:
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. To solve that, add this in FooAdmin
:
readonly_fields = ("date",)
这篇关于Django ModelAdmin - 字段集...表单中缺少字段“日期"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文