The Django Framework's Killer Feature for Java Developers
Code Speaks for Itself
Here is how the model definition for the invoicing application would look like (usually in a file called models.py):
from django.db import models
class Product(models.Model):
name = models.CharField("Name",max_length=30)
price = models.DecimalField("Price", max_digits = 10, decimal_places = 2)
def __unicode__(self):
return "%s - $%s" % (self.name,self.price)
class Customer(models.Model):
first_name = models.CharField("First Name",max_length=50)
last_name = models.CharField("Last Name",max_length=50)
address = models.CharField("Address",max_length=50)
city = models.CharField("City", max_length=50)
state = models.CharField("State", max_length=2)
postal_code = models.CharField("Postal Code", max_length=10)
def __unicode__(self):
return "%s %s" % (self.first_name,self.last_name)
class Invoice(models.Model):
invoice_number = models.CharField("Invoice Number", max_length=30)
customer = models.ForeignKey(Customer)
invoice_date = models.DateField("Date")
def __unicode__(self):
return self.invoice_number
class InvoiceLine(models.Model):
product = models.ForeignKey(Product)
quantity = models.IntegerField("Quantity")
invoice = models.ForeignKey(Invoice)
def __unicode__(self):
return ("%s x %s = $%s") % (self.product,self.quantity,(self.product.price * self.quantity))
A bit less verbose than JPA, I think we would all agree.
Now let's hook up the entities to the admin interface. Customer, Product and Invoice will all get their own CRUD screens, while Invoice Line will be hooked up automatically to its invoice via a master-header relationship.
In a file usually called admin.py we enter:
from models import Customer, Invoice, InvoiceLine, Product
from django.contrib import admin
class InvoiceLineAdmin(admin.TabularInline):
model = InvoiceLine
extra = 1
class InvoiceAdmin(admin.ModelAdmin):
fields = ['invoice_number','invoice_date','customer',]
inlines = [InvoiceLineAdmin]
admin.site.register(Customer)
admin.site.register(Product)
admin.site.register(Invoice,InvoiceAdmin)
That's it! When this is up and running, the following screens are what we get:
Click here for larger image
Figure 1. A Login Screen (with a built-in user registration and authorization system)
Click here for larger image
Figure 2. A Customer Listing Screen
Click here for larger image
Figure 3. A Customer Edit Screen
Click here for larger image
Figure 4. A Product Listing Screen
Click here for larger image
Figure 5. A Product Edit Screen
Click here for larger image
Figure 6. An Invoice Listing Screen
Click here for larger image
Figure 7. An Invoice Edit Screen
Click here for larger image
Figure 8. Entity Audit Trail
Now think how much code and HTML you would have to create to provide the same functionality in any Java Web framework and ask yourself this simple question: isn't Django worth my time?
And we haven't touched on any of the advanced functionality available in the admin app: filtering, mass actions, in-line editing, customizing the edit/list screens, etc.
Python Tooling
When Java developers hear Python, they probably think of old school Unix editors like vi or emacs. Don't fear; there is a great Python plugin for Eclipse called PyDev. This plugin allows you to stay within the comforts of a modern IDE, providing new project wizards, code completion and even refactoring.
Summary
I hope this short introduction to Django explained clearly how its admin interface elevates Web development above the low-level details of HTML/CSS/JavaScript, etc., and lets you focus instead on the business functionality. No other framework I've seen comes close to delivering this level of productivity.
In short, it's worth learning a Python to take advantage of Django. In our development team, we have settled on Java as our language of choice for all of our performance-intensive server applications and Python Django for all of our Web development.
About the Author
About the Author
Jacek Furmankiewicz is a Senior Java EE, Python, Oracle and MySQL developer at Radialpoint. He has 16 years of IT experience in writing enterprise software for a wide variety of industries.Page 2 of 2
This article was originally published on June 29, 2010