如何在Django管理中显示date_created [英] How to show date_created in Django admin

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

问题描述

我在Django 1.5中创建了一个模型,如下所示:

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

Django管理员如下:

 从django.contrib导入admin 
从demo.models导入消息,数字,关系,SmsLog

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

admin.site.register(Number,NumberAdmin)$ b $我相信Django会自动将date_created列添加到数据库中(因为我知道它会按创建时间排序数据条目)在管理控制台)。有没有办法在管理控制台中查看这些时间/日期?我最近去的是 Django教程 StackOverflow ,但我不想创建另一列在Django官方教程的例子中我自己( pub_date ),如果可能的话添加它。有办法做到吗,如果是这样,有人能告诉我怎么办?谢谢!

解决方案

Django不会自动添加一个date_created列。如果你想跟踪创建日期,你必须在模型中声明它。



你可能会得到它的错觉,因为如果你没有指定一个排序在模型中或在模型的管理类中排序,它将默认按主键排序,这将根据创建模型实例的顺序而增加。


I have a model created in Django 1.5 as below:

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

I set up Django admin as below:

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). 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. 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. 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.

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

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