How do I customize Django's Flatpage to display a new field on the change list page of the admin?
In my flatpage admin change list page, mysite.com/admin/flatpages/flatpage/
, I can see the fields:
- URL
- Title
Is there a way to also show the field Site? I associate 开发者_如何学Cmy 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.
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)
精彩评论