Search code examples
djangodjango-admindjango-flatpages

Change field in Django Flatpages Admin


Using Flatpages with the default admin, I need to change the template field from a text input with to select or radio with predefined choices. It's easy to do this with one of my own apps - just use the choices attribute in the model.

I have tried a few things - I will add details about those attempts later if necessary - but does anyone know a nice way to do this?


Solution

  • Define a custom flatpages ModelAdmin class which inherits from the default one but uses a custom form. On this form, override the field, using the widget you want. Then unregister the flatpages admin and reregister it with your custom class.

    from django.contrib.flatpages.admin import FlatPageAdmin, FlatpageForm
    
    class MyFlatpageForm(FlatpageForm):
        template = forms.ChoiceField(choices=MY_CHOICES)
    
    class MyFlatPageAdmin(FlatPageAdmin):
        form = MyFlatpageForm
    
    admin.site.unregister(FlatPage)
    admin.site.register(FlatPage, MyFlatPageAdmin)