简体   繁体   English

Django admin.py readonly_fields无法正常工作

[英]Django admin.py readonly_fields not working

I need to show on Django administration the date the object was created, but date field should be disabled of modification. 我需要在Django管理上显示创建对象的日期,但应该禁用date字段的修改。

here is my model on Models.py 这是我在Models.py上的模型

from django.db import models
from django.utils import timezone

class MyModel(models.Model):
    name = models.CharField(max_length=200)
    date_of_creation = models.DateField(default=timezone.now)

here is my admin.py 这是我的admin.py

from django.contrib import admin
from .models import MyModel

class MyModelAdmin(admin.ModelAdmin):
    readonly_fields = ('date_of_creation',)

admin.site.register(MyModel)

The "save() override to set the date of creation" isn't the case here. 这里不是“保存save()以设置创建日期”。

I tried this snippet and django documentation already, but can't find my mistake. 我已经尝试过此代码段Django文档 ,但是找不到我的错误。

Please note that I'm a beginner in Django, and this is my first App. 请注意,我是Django的初学者,这是我的第一个应用程序。

Thank you in advance. 先感谢您。

您需要注册管理类和模型。

admin.site.register(MyModel, MyModelAdmin)

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

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