DateTimeField doesn't show in admin system

Viewed 39150

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?

9 Answers

Can be displayed in Django admin simply by below code in admin.py

@admin.register(model_name)
class model_nameAdmin(admin.ModelAdmin):
 list_display = ['date']

Above code will display all fields in django admin that are mentioned in     'list_display', no matter the model field is set to 'True' for 'auto_now' attribute

I wanted to create an editable time field that defaults to the current time.

I actually found that the best option was to avoid the auto_now or auto_add_now altogether and simply use the datetime library.

MyTimeField = models.TimeField(default=datetime.now)

The big thing is that you should make it's the variable now instead of a function/getter, otherwise you're going to get a new default value each time you call makemigrations, which will result in generating a bunch of redundant migrations.

Related