简体   繁体   English

如何在Django admin中显示date_created

[英]How to show date_created in Django admin

I have a model created in Django 1.5 as below: 我有一个在Django 1.5中创建的模型如下:

class Number(models.Model):
    phone_number = models.CharField("Phone Number", max_length=10, unique=True)

I set up Django admin as below: 我将Django管理员设置如下:

from django.contrib import admin
from demo.models import Message, Number, Relationship, SmsLog

class NumberAdmin(admin.ModelAdmin):
    search_fields = ['phone_number']

admin.site.register(Number, NumberAdmin)

I believe Django add "date_created" column to the database automatically (because I know it sorts the data entries by creation time in admin console). 我相信Django会自动将“date_created”列添加到数据库中(因为我知道它会在管理控制台中按创建时间对数据条目进行排序)。 Is there a way to view those time/dates in admin console? 有没有办法在管理控制台中查看这些时间/日期? The closest I have go to is Django tutorial and StackOverflow ,but I do not want to create another column on my own ( pub_date in Django official tutorial's example) and add it if possible. 我最接近的是Django教程StackOverflow ,但我不想自己创建另一个列(在Django官方教程的示例中为pub_date )并尽可能添加它。 Is there a way to do it and if so, could someone show me how to? 有没有办法做到这一点,如果是这样,有人可以告诉我如何? Thank you! 谢谢!

Django does not automatically add a date_created column. Django不会自动添加date_created列。 If you want to track the creation date, you have to declare it in your model. 如果要跟踪创建日期,则必须在模型中声明它。

You may be getting the illusion that it does because if you do not specify a sort order in the model or in the admin class for the model, it will default to sorting by primary key, which will increase according to the order the model instances were created. 您可能会得到它的错觉,因为如果您没有在模型或模型的管理类中指定排序顺序,它将默认按主键排序,主键将根据模型实例的顺序增加创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM