The (Python) Magic of Django: A Tour of the Codebase
2022-09-22, 16:50–17:20 (Europe/Lisbon), Auditorium

This tour of Django's codebase will explain how Django's magic is implemented, covering some of its metaclasses and other special techniques.


Learning how Django worked under the hood was the thing that gave me the confidence, and more importantly some guidance, to use Python's advanced functionality. Advanced things like 'dunder' methods, metaclasses, and even things like iterating through the contents of a module.

I'll cherry-pick and explain some of Django's more advanced code features, things like:

  • How Django loads code dynamically to put your application and configuration together.
  • The ModelBase metaclass, and how it attaches magic functonality to your Model subclasses.
    • How __init_subclass__ can be used to provide some or all of this behaviour in Python 3.6+
  • How kwargs are handled to configure queries and generate SQL.

As we look at the code, I'll also cover some of the design decisions that make Django such a joy to work with, without having to understand the implementation details hidden under the hood.

Video: https://youtu.be/92l_MyGIulA

Mark Smith is a Senior Developer Advocate for MongoDB, and lives in Scotland.
He's been coding and teaching coding for over 25 years.
These days he specialises in MongoDB, Python, and Rust.
When he's not sitting in front of a computer, you'll find him 3D printing, building mechanical keyboards, or doing DIY.