Skip to main content

Aggregating data in Django using database views

Level:
intermediate
Duration:
30 minutes

Abstract

Aggregating information is a common Django task, but using the aggregate method can be a bit cumbersome and in the case of large database tables, pretty slow as well. I will introduce the library django-pgviews-redux, which adds first-class support for database views (with Postgres), making that task much simpler.

With that library, database views are wrapped around models, meaning you get many of the features you rely on with models for free, like querysets and filtering on those, admin, and any other feature which works with models. Defining a view is almost as simple as defining a model, by specifying what fields there are for the model and defining the SQL.

This talk will walk through examples of aggregation in Django, and then show how one could simplify those examples using the library. Finally, we will get to materialized views as well, which stores the aggregation almost like a table in the database, providing big speed improvements on aggregation on large tables.


The speaker

Mikuláš Poul

Mikuláš Poul

I’ve now been coding more years than not, and have been working with Django professionally for 10 years. Particular areas of interest to me are Django’s ways of interacting with the database, testing using pytest, and data processing pipelines. I’ve written a couple minor public libraries, am maintain a couple more, and have written a lot of tooling internally for the SaaS application my company is developing.

Born and raised in Prague, Czech Republic, now I live in London after studying software engineering at FIT CTU and data science at UCL in London.