Display an inherited Field in Django 4.0 with ModelAdmin

I lookup for the solution but I believe I'm doing something wrong or in Django 4 is different. The created_at and updated_at fields doesn't appear on admin. I don't know what I'm skipping.

The Parent class

class TimeStampMixin(models.Model):
  class Meta:
      abstract = True
  created_at = models.DateTimeField(auto_now_add=True)
  updated_at = models.DateTimeField(auto_now=True)

  def __str__(self) -> str:
      return f'{self.created_at} - {self.updated_at}'

The class that inherited from TimeStampMixin

class Post(TimeStampMixin):
    STATUS_CHOICES = (
        ('draft', 'Draft'),
        ('published', 'Published'),
    )
    title = models.CharField(max_length=200)
    body = models.TextField()
    category = models.ForeignKey('Category', on_delete=models.SET_NULL, null=True)
    # tags = models.ManyToManyField('Tag')
    tags = TaggableManager()
    objects = models.Manager() # The default manager.
    slug = models.SlugField(max_length=250,
                            unique_for_date='publish')
    author = models.ForeignKey(User,
                              on_delete=models.CASCADE,
                              related_name='blog_posts')
    publish = models.DateTimeField(default=timezone.now)
    status = models.CharField(max_length=10,
                              choices=STATUS_CHOICES,
                              default='draft')
    
    published = PublishedManager() # Our custom manager.


    class Meta:
        ordering = ('-publish',)
        
    def __str__(self):
        return self.title
    
    def get_absolute_url(self):
        return reverse('blog:post_detail',
                        args=[self.publish.year,
                                self.publish.month,
                                self.publish.day, self.slug])

the admin specification in admin.py file

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    list_display = ('title', 'slug', 'author', 'publish', 'status', 'created_at', 'updated_at')
    list_filter = ('status', 'created_at', 'publish', 'author')
    search_fields = ('title', 'body')
    prepopulated_fields = {'slug': ('title',)}
    raw_id_fields = ('author',)
    date_hierarchy = 'publish'
    ordering = ('status', 'publish')

the result

enter image description here


Solution 1:

The problem is that your created_at and updated_at should be readonly_fields. Try the following

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    list_display = ('created_at', 'updated_at')
    # your code
    readonly_fields = ('created_at', 'updated_at')

It will work