Blog: blog.bmispelon.rocks

Django: Writing Robust Tests for N+1 Problems

Aug. 7, 2025 » blog.bmispelon.rocks » [Archived Version]

Django: Writing Robust Tests for N+1 Problems Published on August 7th TLDR; For those in a hurry (or more realistically for future me who will want to find this again in 6 months), here's the important bit: class BookListViewTestCase(TestCase): def test_view_query_count(self): """ Make sure that listing books always triggers the same number of database queries no matter how many books there are. """ Book.objects…

Read More

Wagtail: Annotating a Parent Attribute on a Page QuerySet

Aug. 1, 2025 » blog.bmispelon.rocks » [Archived Version]

Wagtail: Annotating a Parent Attribute on a Page QuerySet Published on August 1st I've been working on a Wagtail project recently where I found myself wanting to filter a queryset of pages based on the property of each row's parent. I found a solution using annotations and Django's database functions which I thought was neat. TLDR; from django.db.models import OuterRef, Subquery from django.db.models.functions import Reverse, Substr from wagtail.…

Read More

Using a temporary storage backend inside a Django test

Dec. 9, 2024 » blog.bmispelon.rocks » [Archived Version]

Using a temporary storage backend inside a Django test Published on December 9th Today I ran across an issue involving storages and staticfiles during tests. I was surprised not to find any quick solution online so here's my quick attempt at a solution. First the code, then some explanations: import shutil import tempfile class TempDirMediaRootMixin: @classmethod def setUpClass(cls): cls.tmpdir = tempfile.mkdtemp() super().setUp…

Read More

GenericForeignKey Deep Filtering

Nov. 25, 2024 » blog.bmispelon.rocks » [Archived Version]

GenericForeignKey Deep Filtering Published on November 25th One of the many "batteries" Django comes with is GenericForeignKey (often shortened to GFK). I'm not necessarily the biggest fan of that particular battery (that might be a topic for another post?), but it's hard to deny that GFKs can enable some pretty nifty use-cases. Recently at work I was tasked with implementing a kind of deep filtering of a model that used a GFK, and came up wi…

Read More

Find git commits that delete files

Nov. 7, 2024 » blog.bmispelon.rocks » [Archived Version]

Find git commits that delete files Published on November 7th While working on a Django ticket, I found myself doing some git archeology and learned a new (to) me flag for git log which I thought I'd share. The problem statement I wanted to find commits that had removed or renamed a file inside a specific directory (docs/ in this case). I've been doing git archeology for long enough that I know that when the problem statement starts …

Read More

Manually setting a field when saving a ModelForm

Nov. 4, 2024 » blog.bmispelon.rocks » [Archived Version]

Manually setting a field when saving a ModelForm Published on November 4th Sometimes when using ModelForms, you want to set some fields manually on the instance. Consider for example the case where you have a blog app with an Article model, and you want to automatically set the current request's user as the article's author. The easy case If an article has a single author, then doing this is pretty straightforwad. Let's say we have this model/…

Read More

Django: Getting a full model instance from a Subquery

May 9, 2024 » blog.bmispelon.rocks » [Archived Version]

Django: Getting a full model instance from a Subquery Published on May 9th This one might be a bit niche, but it took me a bit of trial-and-error and I'm hoping that the technique I describe might be generic enough to be reusable. Who knows, if there's enough interest I could even turn it into a library! EDIT: I've received some positive feedback on Mastodon, so I took some time to package things up and put them on github: django-model-…

Read More

Optional subfactories for Factory Boy

May 3, 2024 » blog.bmispelon.rocks » [Archived Version]

Optional subfactories for Factory Boy Published on May 3rd I often use a library called Factory Boy to create Django models in my tests. It works fine and I mostly like it but there's one thing that annoys me when it comes to nullable foreign keys on Django models. Let's set up some example models to explain: from django.db import models class Author(models.Model): name = models.CharField(max_length=100) email = models.EmailField(required=…

Read More

A Syntax Highlight Mystery

April 26, 2024 » blog.bmispelon.rocks » [Archived Version]

A Syntax Highlight Mystery Published on April 26th While writing some documentation for Django today, I noticed something that I thought was a bit weird inside one of the code samples. Can you spot it? .. code-block:: pycon # Import the models we created from our "news" app >>> from news.models import Article, Reporter # No reporters are in the system yet. >>> Reporter.objects.all() <QuerySet []> I must have scrolled p…

Read More

Etymology, naming things, and off-by-one errors

April 7, 2024 » blog.bmispelon.rocks » [Archived Version]

Etymology, naming things, and off-by-one errors Published on April 18th Have you ever heard of this programming joke? - Why can't programmers tell Halloween and Christmas apart? - It's because they think OCT31 is the same as DEC25 Explaining the joke Always a good sign when you have to explain the joke, isn't it? First off, in case that wasn't obvious: Halloween and Christmas are two holidays that take place on October 31st and December…

Read More