One way to do Django on AppEngine

June 3rd, 2011 Comments Off

Disclaimer: I work for Google.  Any and all opinions expressed or interpreted below are mine own and not that of my employer.

I’ve built dozens webapps in the past 10 years (thats right, i was writing web apps at 13) using all sorts of tools.  It started with raw HTML and spaghetti code perl.  Then it moved onto spaghetti code PHP.  Then it was PHP in a framework.  Then it was asp .NET and finally for the past 3 years or so I’ve used exclusively Django.

So naturally when a friend of mine approached me with an idea for a project using Google’s new prediction API I immediately thought to use Django.

But what’s the fun of doing a project the same way you’ve been doing it for 3 years?  Django + Apache + EC2 is boring.  I figured, I work for Google, perhaps I should be using a Google hosted solution.  It was time to use AppEngine.

Full stop.  Think for a minute.  Django, a highly relational tool built on databases like Mysql and Postgresql, seems fundamentally at odds with the philosophy of AppEngine — non relational, highly distributed.  Fudge!

OK, breathe.  There has to be a way to make this work.  From some research I discovered:

  • You *CAN* use Django + MySQL in AppEngine.  The most common technique I uncovered involves hosting MySQL outside of AppEngine and establishing a secure connection from AppEngine to the remote DB.  This seemed a bit excessive and wasteful — the whole point of this exercise is to use AppEngine!
  • Or, you could use Django-Nonrel

Django-Nonrel is a version of Django that modifies the Django query generator to work ontop of a non-relational database.  Beware though, this has some inherent drawbacks.  Complex joins and inheritance, for obvious reasons, no longer work.  Before continuing any further read through the entire django-nonrel page and all the limitations and make sure you can live with them.  I was able to, you probably (with a bit of work) can too.

That doesn’t solve all our problems though.  We still don’t have a connection between the Django application and the AppEngine datastore.  Thats where DjangoAppEngine comes in.  DAE provides the following, incredibly awesome, capabilities:

  • A connection between the django-nonrel query system and the AppEngine datastore.
  • Automagically sets up AppEngine logging for your Django App.
  • Automagically sets up the Django URL handling system within the AppEngine app — all you need to do is setup your urls.py!
  • Most importantly, provides new hooks within manage.py to allow you Django Shell access to your production AppEngine datastore!  While not critical once your app is deployed, for development and debugging this capability is invaluable!

So, brave adventurer, be off with you!  Explore the wonderful world of building applications with the ever popular python ORM Django ontop of the ever scalable AppEngine platform!   My experience was slow and painful, and involved a lot of head scratching and googling, but in the end all was working.   Read the documentation carefully and bliss shall be yours too!

Link to my app coming soon…