Djangocon: ORM, the sequel - Katie McLaughlin

Tags: djangocon, django

(One of my summaries of a talk at the 2018 european djangocon.)

SQL. The “language to talk to databases in a structured way”.

The ORM. Object Relational Mapper. The magic that makes it all work in django (even though there’s no magic in there).

The talk is about the experience of experienced programmers that, for the first time, have to dive into a django project. She used http://glasnt-orm.us.aldryn.io/ (“unicodex”) as an example.

So. There’s a missing icon on the sample page. You have to debug/fix that as an experienced-programmer-without-django-experience. You’re used to SQL, but not to the django ORM.

You get a tip “use the shell”. Which shell? “The c shell? bash?”. No, they mean manage.py shell. With a bit of _meta and some copy/paste you can get a list of the available models.

You could do the same with manage.py dbshell, which would dump you in the sql shell. List the databases and you get the same answer. Pick a promising table and do a select * from unicodex_codepoint.

You can do the same in the django shell with:

from unicodex.models import Codepoint
Codepoint.objects.all()

The rest of the presentation was a nice combination of showing what happens in SQL and what happens when you use the ORM.

Once you get to the double underscores, for following relations and field lookups, the ORM starts to get more useful and easier than raw SQL:

Design.objects.filter(vendorversion__vendor__name__contains='micro')

It was a fun presentation. I can’t really do it justice in a textual summary: you should go and look at the video. It is much more convincing that way. Yeah, the video is already online: https://www.youtube.com/watch?v=AIke7IZdVJI

There’s a whole part about Q() object and the magic ways in which you can combine it with ~, & and |.

How would it translate to SQL? You can show the latest SQL query:

from django.db import connection
connection.queries[-1]

At the end of the presentation, she went back to the original usecase and started bughunting with the ORM. In the end it was a bug (a unicode ‘bug’ character) at the end of a filename :-)

http://abload.de/img/screen_shot_2014_11_2j3svq.png

Photo explanation: constructing a viaduct module (which spans a 2m staircase) for my model railway on my attic.

 
vanrees.org logo

Reinout van Rees

My name is Reinout van Rees and I program in Python, I live in the Netherlands, I cycle recumbent bikes and I have a model railway.

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):