Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 852
dotty Avatar asked Jun 17 '11 13:06

dotty


People also ask

Where is Django admin?

When you put 'django. contrib. admin' in your INSTALLED_APPS setting, Django automatically looks for an admin module in each application and imports it. This is the default AppConfig class for the admin.

How do I add an administrator to a model?

You can check the admin.py file in your project app directory. If it is not there, just create one. Edit admin.py and add below lines of code to register model for admin dashboard. Here, we are showing all the model fields in the admin site.

What is Django admin in Django?

django-admin is Django's command-line utility for administrative tasks. This document outlines all it can do. In addition, manage.py is automatically created in each Django project.


2 Answers

If you really want to see date in the admin panel, you can add readonly_fields in admin.py:

class RatingAdmin(admin.ModelAdmin):     readonly_fields = ('date',)  admin.site.register(Rating,RatingAdmin) 

Any field you specify will be added last after the editable fields. To control the order you can use the fields options.

Additional information is available from the Django docs.

like image 93
Hunger Avatar answered Oct 08 '22 04:10

Hunger


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.

like image 43
Shawn Chin Avatar answered Oct 08 '22 04:10

Shawn Chin