How to configure Django Flatpage to display a new field on the admin change list page?

On my change list page in the ads list,, mysite.com/admin/flatpages/flatpage/I see the fields:

  • URL
  • Title

Is there a way to show the site field? I link my flat pages to specific sites. The wrong way to do this is by going to the actual Sourcepage source element django/contrib/flatpages/admin.pyand creating a method that will display sites for the Flatpage on the change list page.

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

+3
source share
1 answer

flatpages/admin.py. CustomFlatPageAdmin, FlatPageAdmin.

, customflatpage admin.py , , , .

#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)
+9

Source: https://habr.com/ru/post/1721335/


All Articles