django - 如何在后端显示 Django 时间戳

标签 django django-models timestamp

我正在使用 Django 1.6 作为后端,我想做的是让用户在观看视频时写下他们的评论。我写的字段

datetime = models.DateTimeField(auto_now_add=True)

在 model.py 中,但在 Django 后端中,我没有看到此列,如何让它显示在我的管理中?谢谢!

最佳答案

只需将 readonly_fields 选项添加到您的 admin.py 中:

class CommentAdmin(admin.ModelAdmin):
    readonly_fields = ('datetime',)

如果您在管理类中定义了 fields 选项,您还必须将 datetime 添加到 fields 选项:

class CommentAdmin(admin.ModelAdmin):
    # display datetime in the changelist
    list_display = ('id', 'title', 'datetime')
    # display datetime when you edit comments
    readonly_fields = ('datetime',)
    # optional, use only if you need custom ordering of the fields
    fields = ('title', 'body', 'datetime')

admin.site.register(Comment, CommentAdmin)

更多信息,请查看: https://docs.djangoproject.com/en/1.9/ref/contrib/admin/#django.contrib.admin.ModelAdmin.readonly_fields

关于django - 如何在后端显示 Django 时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36789929/

相关文章:

python - sqlite3.操作错误

python - 查询没有命名直通字段的多对多关系

django - 从 json 中删除字段

MySQL 从 TIMESTAMP 选择恰好 7 天前的行

perl - Perl 代码中的实时时间戳

mysql - 时间戳查询的索引优化

python - 图片未显示在 django 上

django - 如何在 Django 中将 2 个属性一起设置为主键?

python - 提交表单后,两个字段未保存。 Django

django - 使用 Django 上传图像