>NewForms Admin – Flatpages

>NewForms Admin – Flatpages

>Practical Django Projects (Chapter three), talks about defining an admin interface which is all well and good unless you’re using the NewForms Admin branch (which is intended to become part of Release 1.0). So if you’re an early adopter or reading this after the big change, you’ll be needing to do the following.

The reason I bothered writing this was I couldn’t work out how to override the fact that FlatPage had already registered a ModelAdmin (try to register yours throws an error). A pointer from #django on irc and a look at the source code revealed that unregister was the baby you wanted.

from django.contrib import admin
from search.models import SearchKeyword
from django.contrib.flatpages.models import FlatPage

class SearchKeywordInline(admin.TabularInline):
model = SearchKeyword

class FlatPageAdmin(admin.ModelAdmin):
inlines = [
SearchKeywordInline,
]

# We have to unregister it, and then reregister
admin.site.unregister(FlatPage)
admin.site.register(FlatPage, FlatPageAdmin)

5 thoughts on “>NewForms Admin – Flatpages

  1. >Hi Ian,
    I encountered same problem. I tried your code with Django 1.0 Alpha release. It does not work. I got the message “The model FlatPage is not registered”. Did you try CMS sample with Alpha release? If so, could you please write solution for Alpha release?
    Thanks,
    Hideki

  2. >The problem with this method, is that it overrides the original FlatPageAdmin behavior.

    I would suggest that you should derive a new FlatPageAdmin class from the original one (instead of deriving directly from admin.ModelAdmin):

    from django.contrib import admin
    from search.models import SearchKeyword
    from django.contrib.flatpages.models import FlatPage
    from django.contrib.flatpages.admin import FlatPageAdmin

    class SearchKeywordInlineAdmin(admin.StackedInline):
    model = SearchKeyword

    class FlatPageNewAdmin(FlatPageAdmin):
    inlines = [SearchKeywordInlineAdmin,]

    admin.site.unregister(FlatPage)
    admin.site.register(FlatPage, FlatPageNewAdmin)

    This keeps the show/hide Advance Options behavior of the original FlatPages admin page.

  3. >Hideki-
    I had the same problem. I’m not sure why this solves the problem, but putting this code in its own admin.py file (separate from the original search models.py file) fixed it for me.

  4. >As mentioned above:
    from django.contrib import admin
    from search.models import SearchKeyword
    from django.contrib.flatpages.models import FlatPage
    from django.contrib.flatpages.admin import FlatPageAdmin

    class SearchKeywordInlineAdmin(admin.StackedInline):
    model = SearchKeyword

    class FlatPageNewAdmin(FlatPageAdmin):
    inlines = [SearchKeywordInlineAdmin,]

    admin.site.unregister(FlatPage)
    admin.site.register(FlatPage, FlatPageNewAdmin)

    This above works with Django version 1.1 alpha 1 SVN-10088.

    Thank you, could not find this in the otherwise very good Django documentation.

  5. Yes, I’ve seen that. But as I look at images from camears with backlit CMOS sensors, they are really not great. Ok perhaps not offering any advantage’ is an exaggeration, these new sensors may offer perhaps 0.5 stop advantage, but it’s nothing to get excited about. I mean images from a small size sensor will still look infinitely poorer than, say, an APS-C sensor at high ISO.

Leave a Reply

Your email address will not be published. Required fields are marked *