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?
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.
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.
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.
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.
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.
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