Practical Django Projects and Django 1.0

Posted Thursday 25th September, 2008

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.

Included in: Books, Development, Django, Python, Web

Categories:

  1. Accessibility
  2. Ajax
  3. Apache
  4. API
  5. Architecture
  6. Books
  7. Browsers
  8. CMS
  9. CouchDB
  10. CSS
  11. Design
  12. Development
  13. Django
  14. Email
  15. Events
  16. Gaming
  17. Grammar
  18. Hardware
  19. HTML
  20. HTTP
  21. Humour
  22. Idea
  23. Information Architecture
  24. JavaScript
  25. jQuery
  26. Life
  27. Linux
  28. Literature
  29. Mac OS X
  30. Meme
  31. Microformats
  32. Monday
  33. MySQL
  34. Networking
  35. News
  36. Personal
  37. Photoshop
  38. PHP
  39. Python
  40. Reference
  41. REST
  42. Science
  43. SEO
  44. Server
  45. Site
  46. Sitepimp
  47. Social
  48. Spelling
  49. Syndication
  50. Testing
  51. The Future
  52. Thoughts
  53. Tools
  54. Tutorials
  55. Typography
  56. UI
  57. UNIX
  58. Virtualisation
  59. Web
  60. Web Standards
  61. Widgets
  62. Wii
  63. Writing
  64. Xbox
  65. XHTML