Search code examples
pythondjangodjango-adminadmin

DateTimeField doesn't show in admin system


How come my "date" field doesn't come up in the admin system?

In my admin.py file i have

from django.contrib import admin
from glasses.players.models import *
admin.site.register(Rating)

and the Rating model has a field called "date" which looks like this

date = models.DateTimeField(editable=True, auto_now_add=True)

However within the admin system, the field doesn't show, even though editable is set to True.

Does anyone have any idea?


Solution

  • I believe to reason lies with the auto_now_add field.

    From this answer:

    Any field with the auto_now attribute set will also inherit editable=False and therefore will not show up in the admin panel.

    Also mentioned in the docs:

    As currently implemented, setting auto_now or auto_now_add to True will cause the field to have editable=False and blank=True set.

    This does make sense, since there is no reason to have the field editable if it's going to be overwritten with the current datetime when the object is saved.