« OSGi takes on Server Side Java and SOA. | Main | HTML 5 : We don't need no XHTML »

July 17, 2007

Django: Getting an administrative interface for almost free.

I believe in this day in age we can safely say the majority of web applications are backed by a database, and we use everything from ODBC, JDBC, EJBs, ADO.NET, ORMs or you name it, just to tailor the data and present it to users, unfortunately this process is often twofold for most systems, one dedicated to what end users will be able to see and input and a second to what administrative users will be able to see and modify. Seems the people behind Django -- the Python framework -- put a lot of thought into this process, providing an administrative interface for almost free.

[Entry continues to the left and below ad ]

Developing an application under pressure -- and aren't they all -- can leave many features hanging on the TODO list endlessly, but if I've seen one feature which tends to make it to the end of the list constantly, its the administration interface for managing data stored in a database.

For obvious reasons, obtaining and storing data from end users takes precedence over having a comprehensive web interface for managing it and analyzing, at least in the first project iterations. As a geek, I think I've accessed a database directly more than I care to remember just to see what end users were doing, doing raw "SELECT's" or "UPDATE's" to get a peek, instead of creating a bona-fide administrative web interface. Wouldn't it be nice to get an administrative interface straight out of the box ? Well, Django will give you a pretty good one almost free of coding.

Let say you have the following model for you application :

class Book(models.Model):
    category = models.ForeignKey(Category)
    title = models.CharField(maxlength=150)
    htmllink = models.TextField()
    comment = models.CharField(maxlength=200)
    publication_date = models.DateField()

The previous syntax is a Python/Django object model which automates all the SQL operations(Create, Read,Update, Delete) needed to power whatever DB you are using for your app. The approach is identical to the CRUD / ORM mechanism used by Ruby on Rails and Turbo Gears, nothing so unique about that though.

But now modify the previous model to read like the following listing :

class Book(models.Model):
    category = models.ForeignKey(Category)
    title = models.CharField(maxlength=150)
    htmllink = models.TextField()
    comment = models.CharField(maxlength=200)
    publication_date = models.DateField()
     class Admin:
             pass

With this small modification you will get a default web administrative interface like the following screen(s) :

Here you will see a list with all the 'Book' objects stored in the database, with a link to add new instances, as well as modifying existing books, as well as deleting them (a.k.a CRUD). Identical to Ruby on Rails 'New/Show/Edit/Destroy' links for each model huh ? Not really, the big difference is that these CRUD actions are permission based, with Django letting you assign certain Users or Groups to access this web based administrative interface along with each object type, this is something which -- last time I looked -- was missing from the other Ruby/Python frameworks. But it doesn't stop there, how about sorting this DB object list in a more friendlier way, try the following few lines:

class Book(models.Model):
    category = models.ForeignKey(Category)
    title = models.CharField(maxlength=150)
    htmllink = models.TextField()
    comment = models.CharField(maxlength=200)
    publication_date = models.DateField()
    class Admin:
    list_display   = ('title', 'category', 'publication_date')
       list_filter    = ('category', 'publication_date')
       ordering       = ('-publication_date',)
       search_fields  = ('title',)

These few lines will give you the following web interface:

In this last interface, you will be able to order the list by three different fields just by clicking in each header column, filter the list all together by two fields, order entries by publication date, and have an open ended search box for all books, not bad for just 4 additional lines of code.

I can't say that I had ever created a web administrative interface this fast and flexible for all my DB data. Of course you can override all the HTML, CSS and layout you see here, and I left out a few configuration and deployment issues which may take you a few hours to sort out, but the provided default administrative features are pretty impressive.

Perhaps Ruby/Rails and Python/TurboGears have catched up to this feature by the time you read this, considering the speed at which all these frameworks are evolving, but if you have experimented the drudgery of creating administrative interfaces for all your web data, Django should be a +1 on your list for this need.

[Comments below ad ]

Posted by Daniel at July 17, 2007 4:37 PM


Comments


Post a comment




Remember Me?

(you may use HTML tags for style)

Track back Pings

Track Back URL for this entry:
http://www.webforefront.com/mtblog/mt-tb.cgi/78.