Prettyprinted

Django, Python and Drunken Ramblings

Archive for the ‘iphone’ tag

Pushing Django Objects To The iPhone

without comments

iPhone application development often involves writing a web service counterpart that the iPhone application uses. A key issue when writing a web service for iPhone’s is to figure out how to communicate your server side objects onto your iPhone device. Luckily Cocoa, the application framework for iPhone development, comes with a predefined XML format that store serialized objects and utility methods for deserializing this format into NSArray and NSDictionary objects.

That file format is called Property List because they often store application properties such as user settings.

This blog post introductes a Django application for serializing Django model objects and regular Python types into Property List XML!

The package is called django-plist and the source is available at Github. It is also available from the Python Package Index, install it with easy_install django_plist.

The README file documents the installation process and how to use the application to serialize Django objects, but I’ll give a small teaser here, just to get your attention!

Say we have a model Blog and we want to expose all Blog objects as an array of objects at one URL endpoint and single Blog objects as a dictionary at another endpoint. Point your Objective-C code to the URLs and read it with [NSArray arrayWithContentsOfUrl] and [NSDictionary dictionaryWithContentsOfUrl]. And voilá your Python objects are available as arrays and dictionaries in Objective-C!

The main interface for django-plist is two generic views, you’ll use these views just as you would use the generic views in Django:

from django_plist.views.generic import plist_array, plist_dict

blog_info = {'queryset': Blog.objects.all()}

urlpatterns = patterns('',
    url('^all_entries/$', plist_array, blog_info),
    url('^single_entry/(?P<object_id>\d+)/$', plist_dict, blog_info),
)

Read about the different ways you can customize the generic views and the parameters they take in the documentation.

If you find django-plist useful, please drop me a line!

Written by steingrd

October 25th, 2009 at 9:07 pm

Posted in django

Tagged with , ,