Archive for the ‘python’ Category

Device Atlas API and Django-Admob

Monday, January 26th, 2009

I dream of the day when others code to my API and I sip scotch.

Until then I started two new projects working on other people’s APIs this week.

First I’ve taken a swing at re-imagining the Device Atlas Python API. The existing Python API does the job but the exposed method calls are ominous (`getTreeFromFile`? `getProperties`?) and the returned dictionary objects leaves a bit to be desired. There is also a rather large chunk of what I would consider cruft for dealing with typed and untyped properties. The new API looks like this:

Check it out under `deviceatlas` on github.

Second I’ve written a Django pluggable app for AdMob ads and analytics. I coded a simple snippet some time ago - this is more of the full meal deal and sets AdMob cookies and other wonderful things. The backend code is based off the Ruby AdMob Gem. Check it out under `django_admob` on github.

Updating an object and all related objects in Django - `update_related_field`

Thursday, January 15th, 2009

In one of our Django projects we needed to transfer ownership of an object and all it’s related objects from one user to another. A solution might look something like this:

  1. Collect all related objects related to the object you want to transfer.
  2. Update the ownership field on all the related objects.
  3. Save all the related objects!

django.db.models.query has a class CollectedObjects that makes this a snap. CollectedObjects works with Model._collect_sub_objects to find all related classes and objects of the object you want to transfer. Then you can use the update queryset method on each different class of the related objects to set them to their new owner. Here is a function update_related_field:

And here it is customized to update the owner property of an object: