django-planet

Blog: Adam Johnson

web https://adamj.eu/
Author Adam Johnson

Django: a pattern for settings-configured API clients

Sept. 4, 2024 » Adam Johnson » [Archived Version]

Here’s an example of a common pattern in Django projects: from acme.api import APIClient from django.conf import settings acme_client = APIClient(api_key=settings.ACME_API_KEY) def order_anvil() -> None: acme_client.anvils.order(...) An API client is instantiated as a module-level variable based on some settings. This approach has some drawbacks …

Read More

Django: build a Microsoft Teams bot

Sept. 3, 2024 » Adam Johnson » [Archived Version]

Recently, I built a Microsoft Teams bot for a client, inside their Django project. It wasn’t fun or easy, but the experience did increase my resiliency as a developer. I also went into this forewarned by my wife, a product manager also known as “the integration queen”, who has …

Read More

Django: avoid “useless use of .all()”

Aug. 30, 2024 » Adam Johnson » [Archived Version]

Here’s a little ORM pet peeve of mine that may deepen your understanding of how QuerySets work. Take this code: Digger.objects.all().filter(height_cm__gt=200) The .all() is unnecessary. It’s equivalent to write: Digger.objects.filter(height_cm__gt=200) Why? The manager, Digger.objects, already refers to …

Read More

Django: rotate your secret key, fast or slow

Aug. 30, 2024 » Adam Johnson » [Archived Version]

Django’s SECRET_KEY setting is used for cryptographic signing in various places, such as for session storage and password reset tokens. This makes keeping it secure a high priority since an attacker with the key could forge things like password reset tokens. If you have leaked your secret key, you …

Read More

Django: create sub-commands within a management command

Aug. 13, 2024 » Adam Johnson » [Archived Version]

argparse, the standard library module that Django uses for parsing command line options, supports sub-commands. These are pretty neat for providing an expansive API without hundreds of individual commands. Here’s an example of using sub-commands in a Django management command: from django.core.management.base import BaseCommand class Command …

Read More

Django: Test for pending migrations

June 22, 2024 » Adam Johnson » [Archived Version]

This post is an adapted extract from my book Boost Your Django DX, available now. Django requires every change to model fields and meta classes to be reflected in database migrations. This applies even to things that don’t typically affect the database, such as Field.choices. When iterating on …

Read More

Django: Introducing django-harlequin, a launcher for Terminal-based SQL IDE Harlequin

May 6, 2024 » Adam Johnson » [Archived Version]

Harlequin is a Terminal-based SQL IDE by Ted Conbeer. It is pretty popular, with over 2,500 GitHub Stars and counting. It looks like this: Harlequin is built with Textual to give a very interactive experience on the terminal. You can click around its UI and use keyboard shortcuts. I …

Read More

Django: An admin extension to prevent state leaking between requests

April 28, 2024 » Adam Johnson » [Archived Version]

Here’s a small protection I added to a project a few years ago. I was considering it again since I saw a similar potential bug in a Django middleware. Long live the ModelAdmin instances Django’s admin site is configured by the ModelAdmin class. You register this per model …

Read More

Python: Diffing unit tests to keep a copy-pasted code in sync

April 25, 2024 » Adam Johnson » [Archived Version]

Copy-paste-tweaking library code feels like a dirty but inevitable programming practice. Often driven by deadlines or other constraints, it seems all projects end up with something copy-pasted in and tweaked for one specific use case. When we find ourselves doing this, it’s essential to consider the long-term maintenance of …

Read More

Python: Make line number paths with inspect

April 24, 2024 » Adam Johnson » [Archived Version]

Many terminals and text editors support what I’ll call “line number paths” of the form <filename>:<lineno>. Opening that path, whether by clicking or passing to a CLI, opens the given file at that line. Python’s inspect module has a couple of functions that can be combined to …

Read More