Django reporting with raw SQL

Experience Level: intermediate

Learn how to implement reporting for your Django application without carefully crafted database migrations and constantly wrestling the ORM.


  • timeslot: Monday, 7th April 2025, 15:00-16:00, Room C
  • tags: web

Adding reporting to your Django application can be challenging. Django lacks support for helpful database features like SQL views and functions, and the ORM mostly gets in the way of phrasing concise and maintainable complex queries. While packages like django-pgviews-redux can help, they still need love and care in order to deal with data model changes.

This talk presents an approach based on raw SQL and separate of Django migrations. Reporting queries can first be designed and tested in any database tool. Then they are integrated into our Django application as SQL view and function by means of management commands. These commands are called during deployment along side the “migrate” command. And finally, we use raw SQL to query these data from inside the Django application.

As an added bonus, these database views and functions can easily be moved to a separate database schema and made accessible to read-only database users and business intelligence applications.

The intended audience are developers with a basic understanding of Django’s data model and the skill to manually phrase simple SQL select queries.


Thomas Aglassinger

Thomas Aglassinger has been using Python in since 2008 for various tasks, such as preparing unstructured data for integration in a data warehouse, helping people to realize their application idea with Django, or implementing open source tools like pygount. He is the founder of Siisurit and a part time employee of Providens Analytics, where Python and Django are a prominent part of the technology stack.

Outside of his professional career he helps organizing the Python user group Graz, likes to take bike trips and is an avid gamer.

blank_avatar