Blog Info | Adam Johnson |
---|---|
Blog website | Link |
July 31, 2025 » Adam Johnson » [Archived Version]
Here’s a little tip based on some work that I did recently. The project has a URL pattern where the first part of the URL matches the current role the user is viewing the site as. Let’s say the roles are “chef”, “gourmand”, and “foodie”—example URLs might …
Read MoreJuly 29, 2025 » Adam Johnson » [Archived Version]
Within Django’s popular admin site, you can override ModelAdmin.get_queryset() to customize the queryset used by the admin views. It’s often used for performance optimizations, such as adding a select_related() call to batch-fetch related objects: from django.contrib import admin from example.models import Book @admin.register(Book …
Read MoreJuly 21, 2025 » Adam Johnson » [Archived Version]
I’ve found it useful, on occasion, to iterate through all registered URL patterns in a Django project. Sometimes this has been for checking URL layouts or auditing which views are registered. In this post, we’ll look at a pattern for doing that, along with an example use case …
Read MoreJune 26, 2025 » Adam Johnson » [Archived Version]
From Django 5.2 (April 2025), the runserver management command outputs a warning: $ ./manage.py runserver ... Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. WARNING: This is a development server. Do not use it in a production setting. Use a production WSGI or …
Read MoreJune 23, 2025 » Adam Johnson » [Archived Version]
I recently released a new package called inline-snapshot-django. It’s a tool for snapshot testing SQL queries in Django projects, described shortly. Snapshot testing and inline-snapshot inline-snapshot-django builds on top of the excellent inline-snapshot, a nifty snapshot testing library. Snapshot testing is a type of testing that compares a result …
Read MoreMay 19, 2025 » Adam Johnson » [Archived Version]
Python comes with two built-in profilers for measuring the performance of your code: cProfile and profile. They have the same API, but cProfile is a C extension, while profile is implemented in Python. You nearly always want to use cProfile, as it’s faster and doesn’t skew measurements as …
Read MoreMay 2, 2025 » Adam Johnson » [Archived Version]
Adam Hill posted a question on Mastodon: he wants a model field that uses choices that doesn’t generate a database migration when the choices change. This post presents my answer. First, we’ll recap Django’s default behaviour with choice fields, a solution with callable choices, and the drawbacks …
Read MoreApril 6, 2025 » Adam Johnson » [Archived Version]
Django 5.2 was released last Wednesday, another exciting step forward for our favourite web framework. It comes with a composite of new features, contributed to by many, some of which I am happy to have helped with. Below is my pick of highlights from the release notes. If you …
Read MoreJan. 9, 2025 » Adam Johnson » [Archived Version]
JavaScript’s import statement lets module scripts import objects from other scripts. For example, you can define a script as a module in HTML: <script type=module src="/static/js/index.js"></script> Then that file can import from another, like: import { BigBlueButton } from "/static/js/lib/buttons.js"; // ... This …
Read MoreJan. 8, 2025 » Adam Johnson » [Archived Version]
You might see this message when running pytest on your Django project: $ pytest ... Exception ignored in: <django.core.management.base.OutputWrapper object at 0x108576170> Traceback (most recent call last): File "/.../django/core/management/base.py", line 171, in flush self._out.flush() ValueError: I/O operation on closed file. The …
Read More