Unjoinify, taming the sql beast - Matt Westcott (djangocon.eu)

Tags: djangocon, django

Unjoinify helps with deeply nested sql data sets. He doesn’t have a full solution for “eager loading” and the “n+1 query problem” and “select_related”, but unjoinify does help with it.

With nested data sets it is common to see structures like this in your template:

{% for festival in festivals %}
  {% for movie in festival.nominations %}
    {% for actor in movie.actors %}

And in the end django toolbar will tell you you’ve just done 500 queries.

You can do something with .grouper (see template built-ins), but that’s also not optimal.

You could do it with a nice multi-line hand-crafted tedious raw sql query. That’s tedious, but not impossible. What’d be nicer is to have some tool do all this for us. That’s where unjoinify comes in.

Unjoinify uses django’s double-underscore special name idea to specify what you want out of the database. Unjoinify figures out how to get it out of the database:

nomination__movie__id
nomination__movie__title
nomination__movie__actors__name

This means long names, so there’s another trick to get it out in a nicer format. (Note: I might have written this down incompletely/incorrectly, so check the documentation.)

It uses python’s built-in itertools.group_by() to make iteration over similar items at the same level easier.

You really have to know what you’re doing if you use unjoinify. If you know your way around sql and want to get down to optimizing your queries, unjoinify can help you nicely.

He invited us to also look at django-queryset-transform that seems to try and solve the same problem.

Big crane for new railway tunnel in Delft
 
vanrees.org logo

About me

My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):