Skip to content
This repository has been archived by the owner on Sep 17, 2023. It is now read-only.

03 Customize admin

scotwk edited this page May 8, 2015 · 1 revision

When listing your notes, the admin just shows Note object for each note. That's not too helpful. This view can be customize in many interesting ways. (Full documentation at: https://docs.djangoproject.com/en/1.8/ref/contrib/admin/). We'll play with a few of these now.

Define a custom admin class in note/admin.py:

class NoteAdmin(admin.ModelAdmin):
    list_display = ('title', 'pub_date', 'was_published_recently')
    list_filter = ['pub_date']

# Replace your other register call with this line:
admin.site.register(Note, NoteAdmin)

Also, we referred to a new method. Add this method in your Note model in note/models.py:

def was_published_recently(self):
    return self.pub_date >= timezone.now() - timedelta(days=1)

Now the admin list will appear much more useful. We didn't customize the page to create/edit notes, but options are available for that as well (see docs above).