How do I customize Django's Flatpage to display a new field on the change list page of the admin?

StackOverflow https://stackoverflow.com/questions/1639866

  •  08-07-2019
  •  | 
  •  

Question

In my flatpage admin change list page, mysite.com/admin/flatpages/flatpage/, I can see the fields:

  1. URL
  2. Title

Is there a way to also show the field Site? I associate my flatpages to specific sites. The bad way to do it is by going to the actual Flatpage admin source django/contrib/flatpages/admin.py and create a method which will display sites for a Flatpage on the change list page.

I am basically looking for a way to overwrite a django.contrib application on the admin side.

Was it helpful?

Solution

You don't need to edit flatpages/admin.py. Instead, create a CustomFlatPageAdmin that inherits from the default FlatPageAdmin.

You might want to create a customflatpage app for the following admin.py file, or perhaps you already have a utilities app that you can add it to.

#admin.py
from django.contrib import admin

from django.contrib.flatpages.models import FlatPage
from django.contrib.flatpages.admin import FlatPageAdmin

def get_sites(obj):
    'returns a list of site names for a FlatPage object'
    return ", ".join((site.name for site in obj.sites.all()))
get_sites.short_description = 'Sites'

class CustomFlatPageAdmin(FlatPageAdmin):
    list_display = ('title', 'url', get_sites)

#unregister the default FlatPage admin and register CustomFlatPageAdmin.
admin.site.unregister(FlatPage)
admin.site.register(FlatPage, CustomFlatPageAdmin)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top