More on Django and the iPhone
So a lot have been writing about my installation of Django installed on the iPhone and what its potential uses are. The most intriguing to me has been the use of django as a full fledged framework for on/off-line applications where edge network usage just seems silly.
So I started working on one. I’ve been working on an online flight database for a while now and decided that it’d be cool to create an iPhone application that does the same that can sync with the main database when we become connected and want to push these records up.
So here’s the plan:
Create a launchd script to start python manage.py runserver when the phone wakes / boots.
There’s a good tutorial here on how to create your own launchd plist files, I just link mine to a program that sets my PYTHONPATH and starts manage.py runserver.
Next, I install a django database in /var/root/il/data called il.db.One cool trick you can use in your django applications is to put this in your settings file:
DATABASE_ENGINE = 'sqlite3' DATABASE_NAME = os.path.join(os.path.dirname(__file__), 'data', 'il.db')
That way your database can live in your app directory and even be synced with SVN, etc.
NOTE: The same trick can be used as well for TEMPLATE_DIRS, and keeps your application space nice and tightly packaged.
Next we’ll create our application. This for me is a simple model since all I want to do is capture the date, the field I took off from and the field I landed at and the total time. It’s not designed to be an end-all-be-all, but it’s just to get something down right there so I can add it later.
class Flight(models.Model):
to = models.CharField(maxlength=5)
from = models.CharField(maxlength=5)
date = models.DateField()
total_time = models.DecimalField()
## I know this isn't the correct syntax, but you get the idea.
uploaded = models.BooleanField(default=False)
class Admin:
list_display = ('date', 'to', 'from', 'total_time', 'uploaded')
def __str__(self):
return "%s - %s - %s" % (self.from, self.to, self.date)
Next, we’ll need a model to store all of our additions:
class UnsavedFlight(models.Model): pickled_flight = models.TextField()
That’s it!
So then, in our Flight model, we overload save(self) (or you could wire this to a signal, whichever you prefer) to pickle the serialized response to our UnsavedFlight model. Now here is where you could do some cool things like use urllib to attempt to open a connection to your site, and if it times out, you can pickle the response. Or you can do what you like. I chose just to cache locally until I tell the app to sync.
The rest is just a matter of writing the views to push up the pickled data via POST when you request a sync, which then puts them in a queue on your main server to add more information to the flight.
You can do the same with the call database if you want to track your outgoing calls to a webserver.
About this entry
You’re currently reading “More on Django and the iPhone,” written on 08.21.07 at 6pm by Jay and is tagged iphone, django, programming












5 Comments
Jump to comment form | comments rss [?] | trackback uri [?]