Document Actions

Reinout van Rees - Querying Django models: fabulous & fast filtering

Filed Under:

Reinout van Rees talks about querying Django models: fabulous & fast filtering

See the PyGrunn website for more info about this one-day Python conference in Groningen, The Netherlands.

Goal: show what is possible. Everything is in the Django documentation. Just remember a few things you see here.

Example case: time registration system. Everyone seems to do this. A Person belongs to a Group. A Booking belongs to a Project and a Person.

The Python ORM gives you a mapping between the database and Python.



basic filtering:


specific name:


case insensitive searching for part of a name:


name starting with:


without group:


Filtering strategy:

  • sometimes .exclude() is easier
  • you can stack: .filter().filter().filter()
  • query sets are lazy: only really executed at the moment you need it.
  • just assign the query to a variable, to make complicated queries more understandable
  • start with the model you want


  • select_related: does a big join in SQL so you get one set of results

  • prefetch_related: does one query for one table, and then one query to get all related items

  • if you need only one or two fields, Django does not need to instantiate a model, but can give you a plain dictionary or list instead:

    Person.objects.filter(group__name='Systemen').values('name', 'group__name')
    Person.objects.filter(group__name='Systemen').values_list('name', 'group__name')
    Person.objects.filter(group__name='Systemen').values_list('group__name', flat=True)

Annotation and aggregation:

  • annotate: sum, count, avg
  • aggregation
  • groupby via values (bit of a weird syntax)

Aggregation gives totals:

from django.db.models import Sum

Annotation adds extra info to each result row:


Group bookings by year, give sums:

).values('booked_by__name', 'year_week__year'

Practice this with your own code and data! You'll get the gang of it and get to know your data and it is fun.

If you need to do special queries, you can create a sub query yourself:

from django.db.models import Q
query = Q(group__name='Systemen')

You can write filters that way that are not in default Django.

Twitter: @reinoutvanrees