Last evening I updated my django source to the latest SVN and found a surprise. The latest set of backwards incompatible changes were affecting my project. Specifically the new ModelAdmin moves were making things hard for me. Given that I have a few applications in django, I decided to write a script that would help speed up the process. What I ended up with was a simple gawk script that processes the models.py file and outputs the code for new ModelAdmin classes. I still have to manually remove the original admin classes because I didn’t want this to mess up things, but not having to look through the models and copy-pasting should save you some time.

For instance, if your models.py has three models-Tag, TaggedItem and NodeGroup-with Admin classes defined then this script will print out…

$ awk -f admin_model.awk models.py
class NodeGroupAdmin(admin.ModelAdmin):
                date_hierarchy = 'created_on'
                list_per_page = 25
                search_fields = ('title', )
                list_display_links = ('title',)
                list_display = ('id', 'title', 'created_on', 'updated_on', 'published', 'group_type',)
                list_filter = ('published','group_type',)
class TaggedItemAdmin(admin.ModelAdmin):
                pass
class TagAdmin(admin.ModelAdmin):
                pass
admin.site.register(Tag, TagAdmin)
admin.site.register(TaggedItem, TaggedItemAdmin)
admin.site.register(NodeGroup, NodeGroupAdmin)
$

I have added the usage instructions at the top of the script, and here is the script.

Back to blog...