DateTimeField不显示在管理系统中 [英] DateTimeField doesn't show in admin system

查看:119
本文介绍了DateTimeField不显示在管理系统中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的admin.py文件中我有

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

,评分模型有一个名为date的字段,看起来像这样

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

但是在管理系统中,即使可编辑设置为 True



有人有任何想法吗?

解决方案

我相信使用 auto_now_add 字段说谎。



这个答案


任何带有auto_now属性
集合的字段也将是我nherit editable = False
,因此不会显示在
管理面板中。


a href =https://docs.djangoproject.com/en/dev/ref/models/fields/#datefield =nofollow noreferrer>文档:


如当前实现的那样,将
auto_now或auto_now_add设置为True将
导致该字段具有editable = False
和blank = True。


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


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

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)

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

Does anyone have any idea?

解决方案

I believe to reason lies with the auto_now_add field.

From this answer:

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

Also mentioned in the docs:

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.

这篇关于DateTimeField不显示在管理系统中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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