I have a class based on TimeStampedModel from django-extentions:
from model_utils.models import TimeStampedModel
class MyClass(TimeStampedModel):
pass
By default in the admin interface the created and modified fields are not displayed in the edition page my_app/myclass/id.
I tried this hack to force the display of the created and modified fields in the edit admin page for MyClass:
from django.contrib import admin
from my_app.models import MyClass
class MyClassAdmin(admin.ModelAdmin):
fields = MyClass._meta.get_all_field_names()
admin.site.register(MyClass, MyClassAdmin)
But this raised the following exception:
Exception Type: FieldError
Exception Value: Unknown field(s) (modified, created) specified for MyClass. Check fields/fieldsets/exclude attributes of class MyClassAdmin.
Any idea how can I display the created and modified fields in the MyClass edition admin interface?
Note 1: MyClass is a model with a lot of fields including ManyToMany fields. I can display all the fields except the created and modified fields from the base class TimeStampedModel.
Note 2: The admin page in reference is the edition page of a row: my_app/myclass/id
The solution is to use the readonly_fields attribute:
from django.contrib import admin
from my_app.models import MyClass
class MyClassAdmin(admin.ModelAdmin):
readonly_fields = ('created', 'modified', )
admin.site.register(MyClass, MyClassAdmin)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With