Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 256
aptwebapps Avatar asked Dec 09 '22 18:12

aptwebapps


1 Answers

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)
like image 63
Daniel Roseman Avatar answered Jan 29 '23 01:01

Daniel Roseman