(A) SQL for Django
2021-06-02, 12:15–13:00 (Europe/Paris), Main Room

Django's ORM is full-featured, simple to use, and well-suited for almost all interactions with your database. But sometimes, to get better performance or to get data in just the format that you need, it is useful to have the full expressiveness of SQL in your Django application.

In this talk, we will look at using SQL with Django. We will see in which situations the use of SQL makes most sense. You will see the power of SQL and the best ways to use SQL with Django.

We will start with a short review of SQL and of use cases for which SQL gives you more fine-grained control and better results than Djangos ORM. Then, we look at raw SQL queries and RawQuerySets. RawQuerySets make it possible to get model instances with custom SQL. Finally, we learn how to directly work with Django SQL connections and use SQL change your data or get results that don't fit any particular model instance.

This talk will show you how to combine SQL and ORM in Django applications.
Both ORM methods and SQL have their place.
ORM and Django's model classes give us a great development experience. We get an easy-to-use and powerful way to define, migrate, and use our database.
SQL gives us access to all the features our database has to offer. It

The talk will be structured as follows:

  • Why care about SQL?
  • Working with data in Django - Common ORM use cases
  • Creating complex queries: Q, F, annotations
  • Creating and Addressing the N+1 query problem with Django ORM.
  • Everything works, so why use SQL?
  • Minimize Database access for maximal Performance
  • Use Features of complex sequel queries; Sub-Selects
  • Benefit from the SQL Know-How and documentation
  • Best of both worlds: Getting objects with raw queries
  • If all else fails: Directly using database connections for updates and analytics
  • Drawbacks of SQL in Django
  • Review: SQL, Django - How and Why

I started working with Python 2.5 around 2006 to build what might a called a micro-service backend for our company's search backend. In continued to use Python when I did Ph.D. research in automated testing, and I have always been happy with the boost in productivity that Python and Django gave me.

Since then, I have continued to use Python. Sometimes as part of a customer project, sometimes to automate tedious tasks, and sometimes just for fun.
Since 2020, I work as a freelancer in multiple roles.

Twitter: https://twitter.com/stefanbaerisch
Website: https://www.stbaer.de/index.html
Github: https://github.com/stbaercom