Practical Django Projects and Django 1.0

I recently purchased a copy of “Practical Django Projects”, by James Bennett, with the intention of diving straight in and learning Django. I’d prepared for this daring feat of code-ninjutsu [yes, that’s the correct spelling] with a crash course in Python via “Dive Into Python”, by Mark Pilgrim.

This appears to have been the correct choice, as I’ve already acquired a confident grasp of the concepts and techniques of Django in less than a few days of playing with it (granted; with the odd reference look-up on http://www.djangobook.com/ and http://docs.djangoproject.com/en/dev/).

However, last night it all went a bit wrong when I upgraded my local version of Django from 0.96 to 1.0…

Most of my issues were due to the fact that some of the code in the examples, included in Practical Django Projects, was now out of date. However, I had expected this (thanks to warnings from Cyril Doussin and Steve Webster) and went about fixing the problems using the informative Porting Guide on the Django documentation site.

Following my extensive fixes, I was still experiencing issues when decoupling the Admin class from its relevant model. In most cases, this was a relatively easy process, but in one particular situation, where I had used edit_inline and needed to update the method to use the new InlineModelAdmin objects, I had a slightly confusing issue: I needed to add the InlineModelAdmin object to one of the django.contrib applications â€” FlatPage â€” to get the SearchKeyword application to work.

After a short period googling, I found an interesting, well-written blog post by Jason Broyles, “Add InlineModelAdmin to a django.contrib app”. Unsurprisingly, it seemed others had had exactly the same issue.

Jason’s final model.py and admin.py files were as follows:

# models.py from django.db import models from django.contrib.flatpages.models import FlatPage class SearchKeyword(models.Model): keyword = models.CharField(max_length=50) page = models.ForeignKey(FlatPage) def __unicode__(self): return self.keyword

# admin.py from django.contrib import admin from cms.search.models import SearchKeyword from django.contrib.flatpages.models import FlatPage from django.utils.translation import ugettext_lazy as _ class SearchKeywordInline(admin.StackedInline): model = SearchKeyword extra = 3 max_num = 6 class FlatPageAdmin(admin.ModelAdmin): fieldsets = ( (None, {'fields': ('url', 'title', 'content', 'sites')}), (_('Advanced options'), {'classes': ('collapse',), 'fields': ('enable_comments', 'registration_required', 'template_name')}), ) list_display = ('url', 'title') list_filter = ('sites', 'enable_comments', 'registration_required') search_fields = ('url', 'title') inlines = [SearchKeywordInline,] admin.site.unregister(FlatPage) admin.site.register(FlatPage, FlatPageAdmin)

Jason followed these examples with the following explanation:

The above admin.py file will make it so the SearchKeyword model is accessible in the FlatPages admin interface. It does this by first creating the SearchKeywordInline class and adding it to a new FlatPage ModelAdmin function. I also didn't want to loose out on any of the extra options from the default django.contrib.flatpages ModelAdmin so I copied lines 6 and 14-20 from it and added the code to my file. Line 23 then unregisters the default FlatPage ModelAdmin and line 24 registers this new one with my inlines included.

This solution fixed my problems, however I was concerned that copying and pasting code from django.contrib.flatpages ModelAdmin to admin.py was a massively inelegant solution. With that in mind, I came up with the following solution using inheritance:

# admin.py from django.contrib import admin from cms.search.models import SearchKeyword from django.contrib.flatpages.models import FlatPage from django.contrib.flatpages.admin import FlatPageAdmin from django.utils.translation import ugettext_lazy as _ class SearchKeyword_Inline(admin.TabularInline): model = SearchKeyword extra = 3 max_num = 6 class FlatPageAdmin(FlatPageAdmin): inlines = [SearchKeyword_Inline] admin.site.unregister(FlatPage) admin.site.register(FlatPage, FlatPageAdmin)

This worked perfectly.

As a final note, I’d just like to thank Jason for posting his solution; without that much needed information, I probably would have spent a lot more time trying to fix the issue than was really required. It just goes to show that developers that blog are a huge benefit to confused developers everywhere.