4

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?

aptwebapps
  • 1,866
  • 1
  • 13
  • 17

1 Answers1

14

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)
Dominic Rodger
  • 97,747
  • 36
  • 197
  • 212
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thanks! That worked, although I had to change template to template_name. – aptwebapps Dec 02 '09 at 15:42
  • 2
    I can't comment so I write to answer Daniel Roseman Where to put that? Should I make new app for that? – robos85 Feb 11 '11 at 20:38
  • 4
    Don't forget `from django.contrib.flatpages.models import FlatPage` – Tom May 15 '12 at 22:16
  • 2
    This has been suggested elsewhere but I keep asking myself where to add this code. You can use flat pages without an app, so where do you make this override if you simply want to change the behavior of all flat pages in admin form? – nicorellius Aug 30 '13 at 17:20