简体   繁体   中英

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.

here is my model on 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

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.

I tried this snippet and django documentation already, but can't find my mistake.

Please note that I'm a beginner in Django, and this is my first App.

Thank you in advance.

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

admin.site.register(MyModel, MyModelAdmin)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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