DateTimeField 不显示在管理系统中

2022-01-25 00:00:00 python django django-admin admin

问题描述

我的日期"字段怎么没有出现在管理系统中?

How come my "date" field doesn't come up in the admin system?

在我的 admin.py 文件中

In my admin.py file i have

from django.contrib import admin
from glasses.players.models import *
admin.site.register(Rating)

评级模型有一个名为日期"的字段,看起来像这样

and the Rating model has a field called "date" which looks like this

date = models.DateTimeField(editable=True, auto_now_add=True)

但是在管理系统中,该字段不显示,即使 editable 设置为 True.

However within the admin system, the field doesn't show, even though editable is set to True.

有人知道吗?


解决方案

我相信原因在于 auto_now_add 字段.

I believe to reason lies with the auto_now_add field.

来自这个答案:

具有 auto_now 属性的任何字段set 也将继承 editable=False因此不会出现在管理面板.

Any field with the auto_now attribute set will also inherit editable=False and therefore will not show up in the admin panel.

文档中也提到过:

按照目前的实施,设置auto_now 或 auto_now_add 到 True 将导致该字段具有可编辑=假和空白=真设置.

As currently implemented, setting auto_now or auto_now_add to True will cause the field to have editable=False and blank=True set.

这是有道理的,因为如果在保存对象时该字段将被当前日期时间覆盖,则没有理由让该字段可编辑.

This does make sense, since there is no reason to have the field editable if it's going to be overwritten with the current datetime when the object is saved.

相关文章